2010-08-27 111 views

回答

3

/是分隔符。

?!negative lookahead.

[a-z]character class(任何在AZ范围字符)

+是前述图案的one-or-more times[a-z]在这种情况下)

:仅仅是结肠字面

它大致意味着“向前看,并确保没有字母字符后跟冒号“。

此正则表达式会更有意义,如果它有串锚的开始:/^(?![a-z]+:/,所以它不会匹配abc:(像其他答案之一说),但没有(^)我不知道如何这是有用的。

2

Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?![a-z]+:)» 
    Match a single character in the range between “a” and “z” «[a-z]+» 
     Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
    Match the character “:” literally «:» 
0

(?!REGEX)negative lookahead的语法。检查链接以获取有关lookahead的解释。

如果模式[a-z]+:出现在当前位置的字符串中,则该正则表达式失败。如果没有找到该模式,则正则表达式会成功,但不会消耗任何字符。

这将匹配123:abc但不abc:

这将在abc:匹配:

+0

不,这是[不正确。](http://rubular.com/r/sGqgeS5yPi)。它仍然匹配'abc:' – Aillyn 2010-08-27 04:49:17

+0

@Aillyn我看不出在lookahead之前或之后如果没有某种模式,正则表达式会是有用的。 – Amarghosh 2010-08-27 04:54:00

相关问题