Axl
Axl

Reputation: 141

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

What RegEx pattern should be used to match CP_ but not CPLAT::CP_?

Upvotes: 1

Views: 226

Answers (4)

ocodo
ocodo

Reputation: 30319

[^:]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.

Upvotes: 1

Alan Moore
Alan Moore

Reputation: 75272

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.

Upvotes: 1

Kyle Alons
Kyle Alons

Reputation: 7135

(?<!CPLAT::)CP_

Uses negative lookbehind

Upvotes: 3

Babak Naffas
Babak Naffas

Reputation: 12581

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

Upvotes: 0

Related Questions