分类:
2008-09-05 11:27:42
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.
表示匹配规则,如\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*