\A and \Z的含义跟"^'' and "$''一样,但当使用/m时,他们不能匹配多次,而此时"^'' and "$''会在每个行分割符中匹配多次。
".''默认匹配任意一个字符,但如果你关闭/s,".''就不会匹配嵌入的行分隔符。
在[url]www.unicode.org[/url] ( [url]http://www.unicode.org/unicode/reports/tr18/[/url] )有TregExpr与行分隔符一起工作的说明:
"^"位于输入字符串的开头,但是,如果/m是开启的,它会立即匹配跟随在\x0D\x0A or \x0A or \x0D后的字符串(如果你使用Unicode版本的TregExpr,那么也可以是\x2028 or \x2029 or \x0B or \x0C or \x85)。注意在\x0D\x0A序列中没有空行。
"$"位于输入字符串的结尾,但是,如果/m是开启的,它会立即匹配在\x0D\x0A or \x0A or \x0D前的字符串(如果你使用Unicode版本的TregExpr,那么也可以是\x2028 or \x2029 or \x0B or \x0C or \x85)注意在\x0D\x0A序列中没有空行。
"."匹配任意一个字符,但是,如果关闭/s,那么"."不会匹配\x0D\x0A and \x0A and \x0D(如果你使用Unicode版本的TregExpr,那么也不会匹配\x2028 and \x2029 and \x0B and \x0C and \x85)
Note that "^.*$" (an empty line pattern) doesnot match the empty string within the sequence \x0D\x0A, but matchs the empty string within the sequence \x0A\x0D.
注意"^.*$"(空行模式)不会匹配中间有\x0D\x0A序列的空字符串,但匹配中间有\x0A\x0D的空字符串。
借助于TregExpr的LineSeparators和LinePairedSeparator属性,可以很轻松处理多行的情形。你可以只使用Unix风格的\n分隔符或者只使用DOS/Windows风格的\r\n或者混合使用(如上描述一样并以默认的意义使用)或者定义你自己的行分隔符。
元字符 – 预定义类
\W a nonalphanumeric 非阿尔发字符
\d a numeric character 数字
\D a non-numeric 非数字
\s any space (same as [ \t\n\r\f]) 任意空格(同[ \t\n\r\f])
\S a non space 非空格
你可以使\w,\d和\s在自定义字符类中。
foob\dr matchs strings like 'foob1r', ''foob6r' and so on but not 'foobar', 'foobbr' and so on
foob[\w\s]r matchs strings like 'foobar', 'foob r', 'foobbr' and so on but not 'foob1r', 'foob=r' and so on
foob\dr 匹配如'foob1r', ''foob6r'等字符串,除了'foobar', 'foobbr'等。
foob[\w\s]r 匹配如'foobar', 'foob r', 'foobbr'等字符串,除了'foob1r', 'foob=r'等。
TRegExpr 使用SpaceChars and WordChars熟悉定义字符类\w, \W, \s, \S,你可以轻松地重定义它。
元字符 – 单词匹配
\B Match a non-(word boundary) 匹配非单词
A word boundary (\b) is a spot between two characters that has a \w on one side of it and a \W on the other side of it (in either order), counting the imaginary characters off the beginning and end of the string as matching a \W.
元字符 – 迭代符
任意一个正在表达式可能跟有其他类型的元字符 - 迭代符。
使用这种元字符可以匹配指定前面字符、元字符或者子表达式出现的次数的模式,
+ one or more ("greedy"), similar to {1,} 出现1次或以上,同{1,}
? zero or one ("greedy"), similar to {0,1} 出现0次或1次,同{0,1},即要么匹配,要么不匹配。
{n,} at least n times ("greedy") 至少n次
{n,m} at least n but not more than m times ("greedy") n≤count≤
*? zero or more ("non-greedy"), similar to {0,}? 要么出现0次,要么出现一次非0
+? one or more ("non-greedy"), similar to {1,}? 要么出现一次,要么出现一次
?? zero or one ("non-greedy"), similar to {0,1}? 要么出现0次,要么出现一次1次
{n}? exactly n times ("non-greedy") 出现n次
{n,}? at least n times ("non-greedy") 要么出现n次,要么大于n
{n,m}? at least n but not more than m times ("non-greedy") 要么出现一次大于n小于m,要么不出现。
所以,{}中的数字,形如{n,m},指定最小的次数n和最大的m。
{n}形式等于{n,n},即匹配确切的n次。
{n,}形式匹配n次或更多次。
对于n或者m的大小没有限制,但更大的数字将消耗更多的内存并降低r.e的执行速度。
如果{}出现在其他上下文,它被认为一个规则字符。
foob.*r matchs strings like 'foobar', 'foobalkjdflkj9r' and 'foobr'
匹配如'foobar', 'foobalkjdflkj9r' and 'foobr'
匹配如'foobar', 'foobalkjdflkj9r',除了'foobr'
foob.?r matchs strings like 'foobar', 'foobbr' and 'foobr' but not 'foobalkj9r'
匹配如'foobar', 'foobbr' and 'foobr',除了'foobalkj9r'
fooba{2}r matchs the string 'foobaar'
匹配'foobaar'
fooba{2,}r matchs strings like 'foobaar', 'foobaaar', 'foobaaaar' etc.
匹配如'foobaar', 'foobaaar', 'foobaaaar'等
fooba{2,3}r matchs strings like 'foobaar', or 'foobaaar' but not 'foobaaaar'
匹配如'foobaar', or 'foobaaar',除了'foobaaaar'
'abbbbc' return 'bbbb', 'b+?' returns 'b', 'b*?' returns empty string, 'b{2,3}?' returns 'bb', 'b{2,3}' returns 'bbb'.
关于"greediness"的解释。"Greedy"是匹配出现的最多的情况,而"non-greedy"只匹配出现最少的情况。比如,'b+' and 'b*'对于'abbbbc'分别返回'bbbb','b {2,3}?'对'abbbbc'返回'bb','b{2,3}'对'abbbbc'返回'bbb'
你可以是所有迭代符为"non-greedy"模式(参阅修改符/g)
元字符 – 可选符
你可以对一个模式使用"|''分割的一系列你指定的可选符,这样fee|fie|foe将匹配目标串中任意的"fee'', "fie'', or "foe''(f(e|i|o)e同)。
foo(bar|foo) matchs strings 'foobar' or 'foofoo'.
Metacharacters – subexpressions
元字符 – 子表达式
(…)也用于定义正在表达式的子表达式(在解析你能找到的子表达式位置后, TRegExpr的MatchPos, MatchLen and Match属性存储了你找到的子表达式位置、长度和匹配,同时用TRegExpr.Substitute替代它们)
First subexpression has number '1' (whole r.e. match has number '0' - You can substitute it in TRegExpr.Substitute as '$0' or '$&').
子表达式按在括号中从左到右顺序编号。
第一个子表达式为'1'(整个正则表达式为'0' - 你可以在TRegExpr.Substitute用'$0' or '$&'替换它)
(foobar){8,10} matchs strings which contain 8, 9 or 10 instances of the 'foobar'
匹配出现的8, 9 or 10个'foobar'
foob([0-9]|a+)r matchs 'foob0r', 'foob1r' , 'foobar', 'foobaar', 'foobaar' etc.
匹配'foob0r', 'foob1r' , 'foobar', 'foobaar', 'foobaar'等
元字符 – backreferences
元字符\1到\9被解释为backreferences。\<n>匹配前面匹配的子表达式#<n>。
(.)\1+ matchs 'aaaa' and 'cc'. 匹配'aaaa' and 'cc'
(.+)\1+ also match 'abab' and '123123' 匹配'abab' and '123123'
(['"]?)(\d+)\1 matchs '"13" (in double quotes), or '4' (in single quotes) or 77 (without quotes) etc
匹配'"13"(两个引号),或者'4'(一个引号)或者77(没有引号)等