正則表達式:符合一定規(guī)則的字符串。 一般不常用。但要是用起來,卻能解決大問題。在這里對正則表達式整理一下。
A compiled rePResentation of a regular expression. 正則表達式的編譯表現(xiàn)形式。
A regular expression, specified as a string, must first be compiled into an instance of this class. The resulting pattern can then be used to create a Matcher object 正則表達式首先要被編譯為Pattern類的對象。可以通過編譯后的Pattern對象創(chuàng)建匹配器Matcher對象。該Matcher對象可以用來匹配字符串。
An engine that performs match Operations on a character sequence by interpreting a Pattern. 通過該指定Pattern的匹配引擎可以對一個字符串進行多種匹配操作,有如下幾種:
The matches method attempts to match the entire input sequence against the pattern. 匹配整個字符串,matches()方法。
The lookingAt method attempts to match the input sequence, starting at the beginning, against the pattern. 始終從頭開頭匹配,找到為止。lookingAt()方法。
The find method scans the input sequence looking for the next subsequence that matches the pattern. 每次重置,從指定index開始查找。常和group()方法連用。
java Pattern Java Matcher Android Pattern Android Matcher
String類中,正則表達式源碼全都按照此步驟,最終都調(diào)用Matcher類方法。 可查看源碼更詳細獲知。
//把正則表達式編譯成模式對象Pattern Pattern p = Pattern.compile("a*b"); //通過模式對象生成相應的匹配器 Matcher m = p.matcher("aaaaab"); //調(diào)用方法進行匹配 boolean b = m.matches();查看API文檔,得知更詳細的寫法。這里列出常用的。
| 規(guī)則 | 含義 | 
| 字符Characters: | |
| x | The character x,字符x | 
| // | The backslash character,反斜線 | 
| /n | The newline (line feed) character (‘/u000A’),另起一行 | 
| /r | The carriage-return character (‘/u000D’),回車 | 
| 字符類Character classes: | |
| [abc] | a, b, or c。 a,b,或者c | 
| [^abc] | Any character except a, b, or c (negation)。除了a,b,c | 
| [a-zA-Z] | a through z or A through Z, inclusive (range)。所有的英文字母 | 
| 預定義字符類Predefined character classes: | |
| . | Any character,通配符。 /.表示字符串 | 
| /d | A digit: [0-9]。所有數(shù)字 | 
| /D | A non-digit: [^0-9]。非數(shù)字 | 
| /s | A whitespace character: [ /t/n/x0B/f/r]。所有空字符 | 
| /w | A Word character: [a-zA-Z_0-9]。所有的英文字母和阿拉伯數(shù)據(jù),和下劃線。 | 
| 邊界匹配Boundary matchers: | |
| ^ | The beginning of a line。開頭 | 
| $ | The end of a line。結(jié)尾 | 
| /b | A word boundary。單詞邊界。/b詳解 | 
| 數(shù)量詞Greedy quantifiers: | |
| X? | X, once or not at all。至多0次 | 
| X* | X, zero or more times。至少0次 | 
| X+ | X, one or more times。至少1次。 | 
| X+ | X, one or more times。至少1次。 | 
| X{n} | exactly n times。只有n次。 | 
| X{n,} | X, at least n times。至少n次。 | 
| X{n,m} | X, at least n but not more than m times。至少n次,不超過m次。 | 
新聞熱點
疑難解答