Regular Expressions: matching anything *but* a specificed string

  • Thread starter Thread starter Jay S
  • Start date Start date
J

Jay S

Hi,

Can't figure out the following:

<concept>
I want to match a specified string followed by any string that is *not* a
different specified string.
</concept>

<specific example>
strA = "value=&quot;"
strB = (anything other than strC, i.e. anything that is not "&quot;")
strC = "&quot;"

Sample string: value=&quot;Some text & punc. here!&quot;
Desired replacement: value="Some text & punc. here!"

It's not sufficient to simply replace "&quot;" with "\"" since I don't want
to replace all instances of "&quot;" in a given file.
</specific example>

Thanks for any help.
Jay
 
That's called "negative lookahead": 'abc(?!def)' will match 'abc' unless
it's followed by 'def'.

Niki
 
When you doing this, just remember, a 'match' is consecutive characters ...
dont fall into the trap of thinking you can 'exclude' a string... what your
actually doing is creating two matches.

Now, as far as your problem, probably something like this will work....
(tested it with expresso, singleline ignore case settings)

The match string is --> (?<pre>.*?)(&quot;)(?<inner>.*?)(&quot;)(?<post>)
the replacement string is --> ${pre}"${inner}"${post}

Pretty easy to modify to do whatever else you might want, I definitely
recommend downloading Expresso, its free and really helps with RegExs.
 
Niki Estner said:
That's called "negative lookahead": 'abc(?!def)' will match 'abc' unless
it's followed by 'def'.

Niki, I tried negative lookahead before posting but couldn't get it to
handle the text after the parens. Thanks, though.

Jay
 
Richard Brown said:
When you doing this, just remember, a 'match' is consecutive characters ....
dont fall into the trap of thinking you can 'exclude' a string... what your
actually doing is creating two matches.

Now, as far as your problem, probably something like this will work....
(tested it with expresso, singleline ignore case settings)

The match string is --> (?<pre>.*?)(&quot;)(?<inner>.*?)(&quot;)(?<post>)
the replacement string is --> ${pre}"${inner}"${post}

Pretty easy to modify to do whatever else you might want, I definitely
recommend downloading Expresso, its free and really helps with RegExs.

Thanks, Richard, that works! What was confusing to me was that you can
exclude specific characters and you can do negative lookaheads, so I thought
you should be able to specify a string to exclude.

Thanks also for the Expresso recommendation; I just downloaded it.

Jay
 
Back
Top