Loading

正则表达式Lookaround--前后查看

 

Like anchors, lookaround groups match at a certain position rather than certain text. Lookahead will try to look forward at the current position in the string, while lookbehind will try to look backward. If the regex tokens inside the group can be matched at that position, positive lookaround will succeed, and negative lookaround will fail. If the regex tokens cannot be matched, positive lookaround fails and negative lookaround succeeds.

Lookaround is mainly used to check if something occurs before or after the match that you're interested in, without including that something in the regex match.

  • Positive lookahead: (?=)Succeeds if the regular expression inside the lookahead can be matched starting at the current position.
  • Negative lookahead:(?!) Succeeds if the regular expression inside the lookahead can NOT be matched starting at the current position.
  • Positive lookbehind: (?<=)Succeeds if the regular expression can be matched ending at the current position (i.e. to the left of it)
  • Negative lookbehind:(?<!) Succeeds if the regular expression can NOT be matched ending at the current position (i.e. to the left of it)

         表示匹配规则,如\w

 

注意:查看匹配只关心结果是否匹配成功,而不存储匹配结果内容(将放弃匹配内容),即0长度匹配,若想存储匹配结果,可用(?=(regex))

 

看个简单的例子,如要匹配长度为6,包含“cat”的单词。

首先,包含6个字母可用 \b\w{6}\b

其次,包含“cat”可用 \b\w*cat\w*\b

两者结合后,得到: (?=\b\w{6}\b)\b\w*cat\w*\b

因为\b\w{6}\b已经保证了只包含6个字符的单词,故

优化下,可得(?=\b\w{6}\b)\w{0,3}cat\w*

posted @ 2008-08-03 20:24  .net's  阅读(408)  评论(0编辑  收藏  举报