Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4485449
  • 博文数量: 192
  • 博客积分: 10014
  • 博客等级: 上将
  • 技术积分: 8232
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-21 00:22
文章分类

全部博文(192)

文章存档

2011年(4)

2009年(14)

2008年(174)

我的朋友

分类:

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.

  • 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*

阅读(1254) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~