城市獵人

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

          正則表達式總結

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

          1、 

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

          字符

          說明

          ^

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

          $

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

          ( )

          標記一個子表達式的開始和結束位置。要匹配小括號,請使用 ""(" 和 "")"

          [ ]

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

          { }

          修飾匹配次數的符號。要匹配大括號,請使用 ""{" 和 ""}"

          .

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

          ?

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

          +

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

          *

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

          |

          左右兩邊表達式之間 "或" 關系。匹配 "|" 本身,請使用 ""|"


          比較{? + *}用法

           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

          主站蜘蛛池模板: 湖州市| 新化县| 精河县| 襄城县| 安庆市| 新乐市| 延津县| 广丰县| 汉源县| 宜宾市| 白朗县| 泾川县| 阿拉善右旗| 吉安县| 嘉兴市| 塔河县| 宁武县| 涿鹿县| 岳普湖县| 宁晋县| 彭阳县| 海南省| 彭州市| 太仆寺旗| 仲巴县| 南通市| 丰都县| 大余县| 商南县| 林口县| 扶绥县| 鸡泽县| 泰宁县| 兴文县| 阿克| 林口县| 浦城县| 仁寿县| 西充县| 乌什县| 昌江|