Flex RegExp正則表達式(轉)
今天突然涉及到flex的正則表達式。
到網上以搜羅,發現已經有很多大蝦整理了很多不錯的使用手冊。故而轉過來,加以修改。轉載的內容如下:
這里收集了一些常用的正則表達式語句,大家用到的時候就不用自己寫了
^\d+$ //匹配非負整數(正整數 + 0)
^[0-9]*[1-9][0-9]*$ //匹配正整數
^((-\d+)|(0+))$ //匹配非正整數(負整數 + 0)
^-[0-9]*[1-9][0-9]*$ //匹配負整數
^-?\d+$ //匹配整數
^\d+(\.\d+)?$ //匹配非負浮點數(正浮點數 + 0)
^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$ //匹配正浮點數
^((-\d+(\.\d+)?)|(0+(\.0+)?))$ //匹配非正浮點數(負浮點數 + 0)
^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$ //匹配負浮點數
^(-?\d+)(\.\d+)?$ //匹配浮點數
^[A-Za-z]+$ //匹配由26個英文字母組成的字符串
^[A-Z]+$ //匹配由26個英文字母的大寫組成的字符串
^[a-z]+$ //匹配由26個英文字母的小寫組成的字符串
^[A-Za-z0-9]+$ //匹配由數字和26個英文字母組成的字符串
^\w+$ //匹配由數字、26個英文字母或者下划線組成的字符串
^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$ //匹配email地址
^[a-zA-z]+://匹配(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$ //匹配url
匹配中文字符的正則表達式: [\u4e00-\u9fa5]
匹配雙字節字符(包括漢字在內):[^\x00-\xff]
匹配空行的正則表達式:\n[\s| ]*\r
匹配HTML標記的正則表達式:/<(.*)>.*<\/>|<(.*) \/>/
匹配首尾空格的正則表達式:(^\s*)|(\s*$)
匹配Email地址的正則表達式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
匹配網址URL的正則表達式:^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$
匹配帳號是否合法(字母開頭,允許5-16字節,允許字母數字下划線):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
匹配國內電話號碼:(\d{3}-|\d{4}-)?(\d{8}|\d{7})?
匹配騰訊QQ號:^[1-9]*[1-9][0-9]*$
下表是元字符及其在正則表達式上下文中的行為的一個完整列表:
\ 將下一個字符標記為一個特殊字符、或一個原義字符、或一個后向引用、或一個八進制轉義符。
^ 匹配輸入字符串的開始位置。如果設置了 RegExp 對象的Multiline 屬性,^ 也匹配 ’\n’ 或 ’\r’ 之后的位置。
$ 匹配輸入字符串的結束位置。如果設置了 RegExp 對象的Multiline 屬性,$ 也匹配 ’\n’ 或 ’\r’ 之前的位置。
* 匹配前面的子表達式零次或多次。
+ 匹配前面的子表達式一次或多次。+ 等價於 {1,}。
? 匹配前面的子表達式零次或一次。? 等價於 {0,1}。
{n} n 是一個非負整數,匹配確定的n 次。
{n,} n 是一個非負整數,至少匹配n 次。
{n,m} m 和 n 均為非負整數,其中n <= m。最少匹配 n 次且最多匹配 m 次。在逗號和兩個數之間不能有空格。
? 當該字符緊跟在任何一個其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面時,匹配模式是非貪婪的。非貪婪模式盡可能少的匹配所搜索的字符串,而默認的貪婪模式則盡可能多的匹配所搜索的字符串。
. 匹配除 “\n” 之外的任何單個字符。要匹配包括 ’\n’ 在內的任何字符,請使用象 ’[.\n]’ 的模式。
(pattern) 匹配pattern 並獲取這一匹配。
(?:pattern) 匹配pattern 但不獲取匹配結果,也就是說這是一個非獲取匹配,不進行存儲供以后使用。
(?=pattern) 正向預查,在任何匹配 pattern 的字 即ζヅ洳檎易址 U饈且桓齜腔袢∑ヅ洌 簿褪撬擔 悶ヅ洳恍枰 袢」┮院笫褂謾?
(?!pattern) 負向預查,與(?=pattern)作用相反
x|y 匹配 x 或 y。
[xyz] 字符集合。
[^xyz] 負值字符集合。
[a-z] 字符范圍,匹配指定范圍內的任意字符。
[^a-z] 負值字符范圍,匹配任何不在指定范圍內的任意字符。
\b 匹配一個單詞邊界,也就是指單詞和空格間的位置。
\B 匹配非單詞邊界。
\cx 匹配由x指明的控制字符。
\d 匹配一個數字字符。等價於 [0-9]。
\D 匹配一個非數字字符。等價於 [^0-9]。
\f 匹配一個換頁符。等價於 \x0c 和 \cL。
\n 匹配一個換行符。等價於 \x0a 和 \cJ。
\r 匹配一個回車符。等價於 \x0d 和 \cM。
\s 匹配任何空白字符,包括空格、制表符、換頁符等等。等價於[ \f\n\r\t\v]。
\S 匹配任何非空白字符。等價於 [^ \f\n\r\t\v]。
\t 匹配一個制表符。等價於 \x09 和 \cI。
\v 匹配一個垂直制表符。等價於 \x0b 和 \cK。
\w 匹配包括下划線的任何單詞字符。等價於’[A-Za-z0-9_]’。
\W 匹配任何非單詞字符。等價於 ’[^A-Za-z0-9_]’。
\xn 匹配 n,其中 n 為十六進制轉義值。十六進制轉義值必須為確定的兩個數字長。
\num 匹配 num,其中num是一個正整數。對所獲取的匹配的引用。
\n 標識一個八進制轉義值或一個后向引用。如果 \n 之前至少 n 個獲取的子表達式,則 n 為后向引用。否則,如果 n 為八進制數字 (0-7),則 n 為一個八進制轉義值。
\nm 標識一個八進制轉義值或一個后向引用。如果 \nm 之前至少有is preceded by at least nm 個獲取得子表達式,則 nm 為后向引用。如果 \nm 之前至少有 n 個獲取,則 n 為一個后跟文字 m 的后向引用。如果前面的條件都不滿足,若 n 和 m 均為八進制數字 (0-7),則 \nm 將匹配八進制轉義值 nm。
示例:(不是實例,兩碼事)
當你要在編程中需要用到正則表達式時,你需要在as程序段定義自己一個目標正則表達式,例如:
private var targetRegExp:RegExp=/^[A-Z]+$/;
然后在處理正則表達式的函數添加如下判斷代碼:
假如匹配的變量是parentString
if(targetRegExp.test(parentString)){
......... //匹配成功處理
}
esle{
......... //匹配失敗處理
}
這樣就OK了
附加:
The test() methodThe test() method of the RegExp class simply checks the supplied string to see if it contains a match for the regular expression, as the following example shows:
var pattern:RegExp = /Class-\w/;var str = "Class-A";trace(pattern.test(str)); // output: true The exec() methodThe exec() method of the RegExp class checks the supplied string for a match of the regular expression and returns an array with the following:
- The matching substring
- Substring matches for any parenthetical groups in the regular expression
The array also includes an index property, indicating the index position of the start of the substring match.
For example, consider the following code:
var pattern:RegExp = /\d{3}\-\d{3}-\d{4}/; //U.S phone numbervar str:String = "phone: 415-555-1212";var result:Array = pattern.exec(str);trace(result.index, " - ", result);// 7-415-555-1212Use the exec() method multiple times to match multiple substrings when the g (global) flag is set for the regular expression:
var pattern:RegExp = /\w*sh\w*/gi;var str:String = "She sells seashells by the seashore";var result:Array = pattern.exec(str); while (result != null){ trace(result.index, "\t", pattern.lastIndex, "\t", result); result = pattern.exec(str);}//output: // 0 3 She// 10 19 seashells// 27 35 seashore String methods that use RegExp parametersThe following methods of the String class take regular expressions as parameters: match(), replace(), search(), and split(). For more information on these methods, see Finding patterns in strings and replacing substrings.