开发者

Simple regex negation

Trying to match ONLY the first character in the sample below.

Sample string: C/C++/Objective C/Objective-C/ObjectiveC/objectiveC

My faulty regex: (?![开发者_运维问答O|o]bjective[ |-]?)C(?!\+\+)

Doh.


Try this:

(?<![Oo]bjective[ -]?)C(?!\+\+)


Corrections are:

  • Use negative lookbehind instead of negative lookahead (the (?<!...) bit).
  • Removed pipe character from character classes (the [...] bits).

It might also be worth adding a pair of \bs either side of the C, since your current regex will match Coconut, BBC, CFML and so on

Also worth pointing out that, inside character classes, the - is special if not the first or last character. Some people prefer to escape it even in these situations, i.e. [ \-], in case a later character is accidentally added after it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜