开发者

RegEx to match a string if it does not follow another string

What Re开发者_开发技巧gEx pattern should be used to match CP_ but not CPLAT::CP_?


(?<!CPLAT::)CP_

Uses negative lookbehind


Also, does anyone have a very simple tutorial like RegEx for Dummies? Is it strange that I code in C++ but cannot grasp RegEx easily?

No, it's not strange. Regex mastery requires a certain mindset that doesn't come naturally. And being able to program, in C++ or any other language, doesn't seem to help--if anything, it's a handicap. There's a good tutorial here, but even the best tutorial will only get you to a pidgin level. If you really want to get your head around regexes, you need The Book.

Another problem is that there's no standard for regexes; every programming language, every framework, every IDE or text editor seems to have its own "flavor" of regex. Some have features that others don't, while some use different syntax to do the same things. That's where The Other Book comes in. Many examples of the kinds of tasks we commonly use regexes for, in several of the most popular flavors, and thoroughly explained.


[^:]CP_

Will find all instances of CP_ that aren't preceeded by a :

use the g option (depending on regex flavor) if you expect more than one CP_ match per line.


I think you want "^CP_" as your regular expression. The ^ tells the expression to check to this patter at the start of the input.

http://www.regular-expressions.info/anchors.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜