<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    瘋狂

    STANDING ON THE SHOULDERS OF GIANTS
    posts - 481, comments - 486, trackbacks - 0, articles - 1
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    apache Regexp

    Posted on 2010-09-04 17:57 瘋狂 閱讀(1974) 評論(0)  編輯  收藏 所屬分類: java apache項目
      Regexp是一個學習這種表達式的好工具。
    Regexp是一個由100%純java正則式處理包,是Jonathan Locke捐給Apache軟件基金會的。 他最初開發這個軟件是在1996年, 它包括完整的Javadoc文檔,以及一個簡單的Applet來做可視化調試和兼容性測試.

    2)RE類regexp包中非常重要的一個類,它是一個高效的、輕量級的正則式計算器/匹配器的類,RE是regular expression的縮寫。正則式是能夠進行復雜的字符串匹配的模板,而且當一個字符串能匹配某個模板時,你可 以抽取出那些匹配的部分,這在進行文本解析時非常有用。下面討論一下正則式的語法。
      為了編譯一個正則式,你需要簡單地以模板為參數構造一個RE匹配器對象來完成,然后就可調用任一個 RE.match方法來對一個字符串進行匹配檢查,如果匹配成功/失敗,則返回真/假值。例如:

    RE r = new RE("a*b");
    boolean matched = r.match("aaaab");

      RE.getParen可以取回匹配的字符序列,或者匹配的字符序列的某一部分(如果模板中有相應的括號的 話),以及它們的位置、長度等屬性。如:

    RE r = new RE("(a*)b"); // Compile expression
    boolean matched = r.match("xaaaab"); // Match against "xaaaab"

    String wholeExpr = r.getParen(0); // wholeExpr will be 'aaaab'
    String insideParens = r.getParen(1); // insideParens will be 'aaaa'

    int startWholeExpr = r.getParenStart(0); // startWholeExpr will be index 1
    int endWholeExpr = r.getParenEnd(0); // endWholeExpr will be index 6
    int lenWholeExpr = r.getParenLength(0); // lenWholeExpr will be 5

    int startInside = r.getParenStart(1); // startInside will be index 1
    int endInside = r.getParenEnd(1); // endInside will be index 5
    int lenInside = r.getParenLength(1); // lenInside will be 4


      RE支持正則式的后向引用,如:

    ([0-9]+)=\1
    匹配 n=n (象 0=0 or 2=2)這樣的字符串

    3)RE支持的正則式的語法如下:
    字符
    unicodeChar  Matches any identical unicode character
    \  Used to quote a meta-character (like '*')
    \\  Matches a single '\' character
    \0nnn  Matches a given octal character
    \xhh  Matches a given 8-bit hexadecimal character
    \\uhhhh  Matches a given 16-bit hexadecimal character
    \t  Matches an ASCII tab character
    \n  Matches an ASCII newline character
    \r  Matches an ASCII return character
    \f  Matches an ASCII form feed character

    字符集
    [abc]  簡單字符集
    [a-zA-Z]  帶區間的 字符集
    [^abc]  字符集的否定

    標準POSIX 字符集
    [:alnum:]  Alphanumeric characters.
    [:alpha:]  Alphabetic characters.
    [:blank:]  Space and tab characters.
    [:cntrl:]  Control characters.
    [:digit:]  Numeric characters.
    [:graph:]  Characters that are printable and are also visible.(A space is printable, but not visible, while an `a' is both.)
    [:lower:]  Lower-case alphabetic characters.
    [:print:]  Printable characters (characters that are not control characters.)
    [:punct:]  Punctuation characters (characters that are not letter,digits, control characters, or space characters).
    [:space:]  Space characters (such as space, tab, and formfeed, to name a few).
    [:upper:]  Upper-case alphabetic characters.
    [:xdigit:]  Characters that are hexadecimal digits.

    非標準的 POSIX樣式的字符集
    [:javastart:]  Start of a Java identifier
    [:javapart:]  Part of a Java identifier

    預定義的字符集
    .  Matches any character other than newline
    \w  Matches a "word" character (alphanumeric plus "_")
    \W  Matches a non-word character
    \s  Matches a whitespace character
    \S  Matches a non-whitespace character
    \d  Matches a digit character
    \D  Matches a non-digit character

    邊界匹配符
    ^  Matches only at the beginning of a line
    $  Matches only at the end of a line
    \b  Matches only at a word boundary
    \B  Matches only at a non-word boundary

    貪婪匹配限定符
    A*  Matches A 0 or more times (greedy)
    A+  Matches A 1 or more times (greedy)
    A?  Matches A 1 or 0 times (greedy)
    A{n}  Matches A exactly n times (greedy)
    A{n,}  Matches A at least n times (greedy)

    非貪婪匹配限定符
    A*?  Matches A 0 or more times (reluctant)
    A+?  Matches A 1 or more times (reluctant)
    A??  Matches A 0 or 1 times (reluctant)

    邏輯運算符
    AB  Matches A followed by B
    A|B  Matches either A or B
    (A)  Used for subexpression grouping
    (?:A)  Used for subexpression clustering (just like grouping but no backrefs)

    后向引用符
    \1  Backreference to 1st parenthesized subexpression
    \2  Backreference to 2nd parenthesized subexpression
    \3  Backreference to 3rd parenthesized subexpression
    \4  Backreference to 4th parenthesized subexpression
    \5  Backreference to 5th parenthesized subexpression
    \6  Backreference to 6th parenthesized subexpression
    \7  Backreference to 7th parenthesized subexpression
    \8  Backreference to 8th parenthesized subexpression
    \9  Backreference to 9th parenthesized subexpression


    RE運行的程序先經過RECompiler類的編譯. 由于效率的原因,RE匹配器沒有包括正則式的編譯類. 實際上, 如果要預編譯1個或多個正則式,可以通過命令行運行'recompile'類,如

    java org.apache.regexp.recompile a*b
    則產生類似下面的編譯輸出(最后一行不是):

    // Pre-compiled regular expression "a*b"
    char[] re1Instructions =
    {
    0x007c, 0x0000, 0x001a, 0x007c, 0x0000, 0x000d, 0x0041,
    0x0001, 0x0004, 0x0061, 0x007c, 0x0000, 0x0003, 0x0047,
    0x0000, 0xfff6, 0x007c, 0x0000, 0x0003, 0x004e, 0x0000,
    0x0003, 0x0041, 0x0001, 0x0004, 0x0062, 0x0045, 0x0000,
    0x0000,
    };
    REProgram re1 = new REProgram(re1Instructions);
    RE r = new RE(re1);

      通過利用預編譯的req來構建RE匹配器對象,可以避免運行時進行編譯的成本。 如果需要動態的構造正 則式,則可以創建單獨一個RECompiler對象,并利用它來編譯每個正則式。注意,RE 和 RECompiler 都不是 threadsafe的(出于效率的原因), 因此當多線程運行時,你需要為每個線程分別創建編譯器和匹配器。


    3、例程

    1)regexp包中帶有一個applet寫的小程序,運行如下:
    java org.apache.regexp.REDemo
    運行后:

    2)Jeffer Hunter寫了一個例程,可以下載。
    3)regexp自帶的測試例程,也很有參考價值。它把所有正則式及相關的字符串以及結果都放在一個單獨的文件 里,在$REGEXPHOME/docs/RETest.txt中。當然,這個例程的運行也要在$REGEXPHOME目錄下。
    cd $REGEXPHOME
    java org.apache.regexp.RETest
    主站蜘蛛池模板: 窝窝影视午夜看片免费| 亚洲成AV人片在线观看无码| 亚洲黄色三级视频| 东方aⅴ免费观看久久av| 国产成人综合亚洲AV第一页| 亚洲暴爽av人人爽日日碰| 免费网站看v片在线香蕉| 亚洲综合色婷婷在线观看| 午夜免费福利网站| 亚洲AV无码成人网站在线观看| 日本一道高清不卡免费| 亚洲爆乳AAA无码专区| 亚洲国产成人精品久久久国产成人一区二区三区综 | 在线观看免费人成视频色| 国产国拍亚洲精品福利| 特级毛片免费观看视频| 国产成人亚洲综合| 久别的草原电视剧免费观看| 亚洲黄色在线观看视频| 希望影院高清免费观看视频| 亚洲精品乱码久久久久蜜桃 | 精品亚洲成A人无码成A在线观看 | 免费播放在线日本感人片| 亚洲AV成人一区二区三区AV| 久久精品一本到99热免费| 亚洲videosbestsex日本| 日韩人妻无码免费视频一区二区三区 | 免费v片在线观看品善网| 久久久久国色AV免费观看| 亚洲精品中文字幕无乱码| 成年人在线免费看视频| 一级毛片免费播放试看60分钟| 亚洲国产精品无码一线岛国| 4399好看日本在线电影免费| 天天综合亚洲色在线精品| 国产AV无码专区亚洲AVJULIA| 久久久久久精品成人免费图片| 亚洲AV无码一区二区三区网址 | 亚洲免费视频一区二区三区| 99精品视频在线视频免费观看 | 一个人看的www免费视频在线观看|