Regex入門3(對java.util.regex包的簡單認識)

簡略地瞭解了下java.util.regex包中的一些類,加深了對regex包使用方法的理解。


閱讀了很多博客後,對regex包的大致內容有了進一步了理解
總結性的說:
(1)首先java.util.regex包主要包含Pattern,Matcher,PatternSyntaxException三個類
(2)Pattern類代表了正則表達式,可以看作是正則表達式的編譯表示
(3)Matcher類是對輸入字符串進行解釋和匹配操作的引擎,根據規則進行匹配
(4)PatternSyntaxException代表一個正則表達式中出現語法錯誤


分開來說:
(1)Pattern類:
Pattern類的構造方法是私有的。要想創建一個Pattern對象,需要調用它的公共靜態編譯函數(Pattern.class中源碼):


  public static Pattern compile(String regex) {
        return new Pattern(regex, 0);
    }

compile函數引用了Pattern類的私有構造函數:

private Pattern(String p, int f) {
        pattern = p;
        flags = f;

        // to use UNICODE_CASE if UNICODE_CHARACTER_CLASS present
        if ((flags & UNICODE_CHARACTER_CLASS) != 0)
            flags |= UNICODE_CASE;

        // Reset group index count
        capturingGroupCount = 1;
        localCount = 0;

        if (pattern.length() > 0) {
            compile();
        } else {
            root = new Start(lastAccept);
            matchRoot = lastAccept;
        }
    }

這個公共靜態編譯函數返回了一個Pattern對象

除此之外,還有一個參數不同的公共靜態編譯函數:

 public static Pattern compile(String regex, int flags) {
        return new Pattern(regex, flags);
    }

以及相應的私有構造函數:

 private Pattern(String p, int f) {
        pattern = p;
        flags = f;

        // to use UNICODE_CASE if UNICODE_CHARACTER_CLASS present
        if ((flags & UNICODE_CHARACTER_CLASS) != 0)
            flags |= UNICODE_CASE;

        // Reset group index count
        capturingGroupCount = 1;
        localCount = 0;

        if (pattern.length() > 0) {
            compile();
        } else {
            root = new Start(lastAccept);
            matchRoot = lastAccept;
        }
    }

(2)Matcher
Matcher類也沒有公共的構造方法,需要通過Pattern.matcher()方法來獲得Matcher對象.
Pattern.class中:

   public Matcher matcher(CharSequence input) {
        if (!compiled) {
            synchronized(this) {
                if (!compiled)
                    compile();
            }
        }
        Matcher m = new Matcher(this, input);
        return m;
    }

Matcher.find()函數: 返回值爲布爾類型,搜索與正則表達式相符的任何子字符串,即發現CharSequence裏所有匹配字符序列。find()像一個迭代器,從頭到尾掃描一遍字符串

start()與end():
如果匹配成功,start( )會返回此次匹配的開始位置,end( )會返回此次匹配的結束位置,即最後一個字符的下標加一

split():
根據正則表達式將輸入的字符串分割爲String數組:
此處可以參考博客: Click Here


傳送門:
Regex入門1 : Click Here
Regex入門2 : Click Here

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章