Michal B.
Michal B.

Reputation: 5719

What's the regular expression to achieve the following?

I am looking for a single regular expression that will replace all replace me in the text with REPLACE ME, but will leave dont replace me untouched.

Text sample:

blah replace me blah
blah dont replace me blah
replace me blah blah
blah blah dont replace me

I know it can be done an infinite number of ways, but I am looking for a solution utilizing single regex search and replace, so post only if you have one :)

I think it can be done using information from this site: http://www.regular-expressions.info/refadv.html, but I get lost when I read about atomic grouping, lookarounds etc.

I will upvote any solution that works and accept the nicest one. Thanks in advance.

EDIT: could you also explain the idea behind the regex when you post one? Would be nice for people that are not very familiar with the syntax.

You can test your regexes here: http://www.regextester.com/. I am not sure if it is not limited though...

Upvotes: 0

Views: 52

Answers (1)

kev
kev

Reputation: 161884

In python/perl/c#:

(?<!dont )replace me

In vim:

:%s/\(dont \)\@<!replace me/REPLACE ME/g

Upvotes: 3

Related Questions