我想捕捉字符串中的字常量。该常量是:什么正则表达式可以在短语中捕获2个确切的'单词'?
- 一个字,接着一个分离器(空白,点,短划线或下划线)
- 另一字
- 然后隔板
- (参见#2)或结束线或字符串。
贪图例如假设我在寻找“鲍勃·1”,在下面的字符串:
Hello, I'm Bob 1 --> Should capture Bob 1
Hello, I'm Bob 11 --> Should capture nothing (Bob 1 is not at the end or followed by a separator)
Hey, it's Bob-1 over there --> Should capture Bob-1
Hey, it's Bob - 1 over there --> Should capture nothing (Bob should be followed only by one separator not 3 like here)
Bob.1 --> Should capture Bob.1
Bob_1 rules! --> Should capture Bob_1
我有一个正则表达式是主要工作:
/Bob[\s._-]1[\s._-]/ig
在第二个列表我不知道如何在可能的字符中添加字符串的末尾......只在最后一行的实况演示下面应该是一个匹配,并且不会被捕获。
查看live demo。
我使用pcre(在PHP中)。
我想补充,你需要检查“鲍勃”之前会发生什么,因为“鲍勃”可能我为什么包括在启动A \ B是一个字符串的一部分,因此。 –
我不需要在我的例子中转义它,因为它是列表的最后一个字符。如果这不是引擎认为它是一个字符范围,你需要逃避它。 – AlexV