如何使用正規表示法-1

其實這應該算是java的觀念, 主要是應用在找尋字串,
例如我們如果要寫一個擷取網頁資訊,
第一個動作一定是打開網頁原始碼,
然後判斷我們要的資訊在什麼地方,
但是要根據什麼去判斷呢?
這時候正規表示式就非常好用了。



假設今天有一個字串,
String htmlCode = "The most essential function of HttpClient is to execute HTTP methods.";

假設你想抓出'd'這個字元, 那麼先跟著以下的步驟,
Pattern pattern = Pattern.compile("d");
Matcher matcher = pattern.matcher(htmlCode);
while(matcher.find()) {
    System.out.print(matcher.group());
}
首先pattern的方法compile()參數放的是正規表示式,
接著利用matcher這個方法來判斷我們的字串是否符合這個正規表示式,
pattern物件會回傳一個Matcher物件,
裡面的group()方法會裝著符合這個正規表示式的字串。

所以我們利用while迴圈將這個物件內符合的字串全部印出來,
結果就會印出字串methods的'd',
如果你的字串夠多夠長,
那麼你就會看到畫面印出一堆"ddddddddddddddd",
代表著我們的字串裡面存在著很多'd'字元。

那麼我們就來討論正規表示式是怎麼一回事?
就如上面的例子來看, 如果想要找出某一個字元,
可以在compile裡面的參數加入你想搜尋的字元,

那如果你想搜尋的不只一個字元呢?
就可以在字串裡面多添加"[]"符號,
Pattern pattern = Pattern.compile("[abcd]");
Matcher matcher = pattern.matcher(htmlCode);
while(matcher.find()) {
    System.out.print(matcher.group());
}
這樣一來, 就會看到畫面印出accd,
這個語法的意思就是找出含有'a'或'b'或'c'或'd'的字元。

可是如果我們要印出某一個範圍的字元, 那麼不就要輸入到天荒地老,
例如我們想找出a到p的值, 那麼就要輸入[abcdefghijklmnop],
拜託~等你打完, 大家都下班了。

所以我們的正規表示式很聰明, 可以讓你輸入某一個範圍,
只要這樣做,
Pattern pattern = Pattern.compile("[a-p]");
Matcher matcher = pattern.matcher(htmlCode);
while(matcher.find()) {
    System.out.print(matcher.group());
}
這個就會印出hemoeenialfncionofplienioeecemehod,
同理, 如果你的字串裡面含有數字, 也可以利用[0-5]之類的語法來作!
例如
String htmlCode = 
   "If the sides of a rectangle are measured as 1.23 meters and 4.56 meters, " +
   "then multiplication gives an area for the rectangle of 5.6088 square meters.";
Pattern pattern = Pattern.compile("[0-5]");
Matcher matcher = pattern.matcher(htmlCode);
while(matcher.find()) {
    System.out.print(matcher.group());
}
印出來就會是1234550的結果。


你有發現, 我們找出來的字串都是小寫的嗎?
沒錯, 正規表示式是有分大小寫的,
如果你想大小寫一起找, 那麼就可以這樣寫,
Pattern pattern = Pattern.compile("[a-pA-P]");
Matcher matcher = pattern.matcher(htmlCode);
while(matcher.find()) {
    System.out.print(matcher.group());
}
印出的結果就是hemoeenialfncionofHpClienioeeceHPmehod。

再來就是反向^這個符號,
它代表著否的意思, 也就是說假設你是打[^a-p]
那麼這個表示式代表除了a到p的字母, 其他的字母數字或符號都給我找出來!
String htmlCode = 
   "If the sides of a rectangle are measured as 1.23 meters and 4.56 meters, " +
   "then multiplication gives an area for the rectangle of 5.6088 square meters.";
Pattern pattern = Pattern.compile("[^a-p]");
Matcher matcher = pattern.matcher(htmlCode);
while(matcher.find()) {
    System.out.print(matcher.group());
}
印出來的結果會是I t ss   rt r sur s 1.23 trs  4.56 trs, t utt vs  r r t rt  5.6088 squr trs.
連空白都找出來了!

有否^就代表著有且&, 但是要用兩個&&來表示,
String htmlCode = 
   "If the sides of a rectangle are measured as 1.23 meters and 4.56 meters, " +
   "then multiplication gives an area for the rectangle of 5.6088 square meters.";
Pattern pattern = Pattern.compile("[a-d&&c-f]");
Matcher matcher = pattern.matcher(htmlCode);
while(matcher.find()) {
    System.out.print(matcher.group());
}
結果是dcddcc

那有沒有或||啊? 當然有,
String htmlCode = 
   "If the sides of a rectangle are measured as 1.23 meters and 4.56 meters, " +
   "then multiplication gives an area for the rectangle of 5.6088 square meters.";
Pattern pattern = Pattern.compile("[a-d||c-f]");
Matcher matcher = pattern.matcher(htmlCode);
while(matcher.find()) {
    System.out.print(matcher.group());
}
結果是fedefaecaeaeeaedaeeadeeecaeaaeafeecaefaeee
不過你有沒有發現, 其實我們不用寫, 它預設[]符號內就是或||了XD