城市獵人

          在一網(wǎng)情深的日子里,誰能說得清是苦是甜,只知道確定了就義無反顧
          posts - 1, comments - 7, trackbacks - 0, articles - 89

          正則表達(dá)式總結(jié)

          Posted on 2008-12-07 19:50 sailor 閱讀(185) 評(píng)論(0)  編輯  收藏 所屬分類: java

          1、 

          在表達(dá)式中有特殊意義,需要添加 """ 才能匹配該字符本身的字符匯總

          字符

          說明

          ^

          匹配輸入字符串的開始位置。要匹配 "^" 字符本身,請使用 ""^"

          $

          匹配輸入字符串的結(jié)尾位置。要匹配 "$" 字符本身,請使用 ""$"

          ( )

          標(biāo)記一個(gè)子表達(dá)式的開始和結(jié)束位置。要匹配小括號(hào),請使用 ""(" 和 "")"

          [ ]

          用來自定義能夠匹配 '多種字符' 的表達(dá)式。要匹配中括號(hào),請使用 ""[" 和 ""]"

          { }

          修飾匹配次數(shù)的符號(hào)。要匹配大括號(hào),請使用 ""{" 和 ""}"

          .

          匹配除了換行符("n)以外的任意一個(gè)字符。要匹配小數(shù)點(diǎn)本身,請使用 ""."

          ?

          修飾匹配次數(shù)為 0 次或 1 次。要匹配 "?" 字符本身,請使用 ""?"

          +

          修飾匹配次數(shù)為至少 1 次。要匹配 "+" 字符本身,請使用 ""+"

          *

          修飾匹配次數(shù)為 0 次或任意次。要匹配 "*" 字符本身,請使用 ""*"

          |

          左右兩邊表達(dá)式之間 "或" 關(guān)系。匹配 "|" 本身,請使用 ""|"


          比較{? + *}用法

           1public class DataMatcher {
           2    public static void main(String[] args) {
           3        String input = "aab ab acb ";
           4        String regex = "e.+?d";
           5        String regex1 = "a*b";
           6        String regex2 = "a+b";
           7        String regex3 = "a?b";
           8        
           9        Pattern p = Pattern.compile(regex1);
          10        Matcher m = p.matcher(input);
          11                
          12        while(m.find()){
          13            System.out.println("match: '" + m.group() + "' start: " + m.start() + " end: " + m.end());
          14        }

          15    }

          16}


          regex1 result:

          1match: 'aab' start: 0 end: 3
          2match: 'ab' start: 4 end: 6
          3match: 'b' start: 9 end: 10



          regex2 result

          1match: 'aab' start: 0 end: 3
          2match: 'ab' start: 4 end: 6



          regex3 result

          1match: 'ab' start: 1 end: 3
          2match: 'ab' start: 4 end: 6
          3match: 'b' start: 9 end: 10



          {.}的用法

          1String regex = "a.*?b";
          2String input = "eaab ab eb acb acsd df ad";
          3

          result:
          1match: 'aab' start: 1 end: 4
          2match: 'ab' start: 5 end: 7
          3match: 'acb' start: 11 end: 14


          {|}的用法
          1String input = "eaab ab eb acb acsd df ad";
          2String regex = "(a.*?)(b|d)";

          result:
          1match: 'aab' start: 1 end: 4
          2match: 'ab' start: 5 end: 7
          3match: 'acb' start: 11 end: 14
          4match: 'acsd' start: 15 end: 19
          5match: 'ad' start: 23 end: 25

          {[]}用法
          1String input = "a b c da ab";
          2String regex = "[ab]";

          result:
          1match: 'a' start: 0 end: 1
          2match: 'b' start: 2 end: 3
          3match: 'a' start: 7 end: 8
          4match: 'a' start: 9 end: 10
          5match: 'b' start: 10 end: 11

          主站蜘蛛池模板: 灵石县| 大名县| 新竹县| 松江区| 淳安县| 息烽县| 章丘市| 孟津县| 云龙县| 揭阳市| 张掖市| 丽江市| 织金县| 治多县| 南溪县| 苍山县| 信丰县| 桦甸市| 轮台县| 河池市| 龙口市| 博乐市| 漳平市| 榆林市| 铜鼓县| 巴楚县| 荔浦县| 佛教| 大英县| 文水县| 郓城县| 通江县| 太保市| 太仓市| 郎溪县| 深圳市| 石河子市| 蓬莱市| 汝阳县| 濮阳县| 陆河县|