Neil Moss
Neil Moss

Reputation: 6818

RegEx help - find substring except when in specific word(s)

A colleague is scanning a large script looking for the substring UAT (case insensitive). Unfortunately, the script contains many references to "valuation", either as a whole word or as part of a field name.

What RegEx search pattern would return matches for UAT as in the following samples?

Many thanks

Neil

Upvotes: 0

Views: 190

Answers (4)

agent-j
agent-j

Reputation: 27913

Edit: This one works for the OP's cases.

Try this expression: uat~(ion)

The SSMS (and Visual Studio) regex has different syntax for negative lookahead: ~(prevent)

If you wanted uat by itself, you can use this: <uat>.

The < and > stand for begining and end of word, respectively.

Upvotes: 1

Bohemian
Bohemian

Reputation: 424983

EDITED to be case insensitive and incorporate new requirement

I think the regex you want is simply:

[Uu][aA][tT](?![iI][oO][nN])

ie uat (any case) not followed by ion (any case)

Upvotes: 0

stema
stema

Reputation: 92976

You can try this

UAT(?!ion)

This is a negative lookahead, that ensures that UAT is not followed by ion like in valuation. I don't know if your tool is supporting this and if this rule is strict enough for your needs.

What you should activate is a case insensitive match.

Upvotes: 0

Zsolt Botykai
Zsolt Botykai

Reputation: 51593

fgrep -i "uat" FILE | fgrep -i -v "valuation"

Sometimes the simplest tools can do amazing things.

But you did not specify the tools you have, the regex flavor (there are many). And what you are searching for can be defined via regex (negative look-(ahead|behind)), but simple string search and negation can be faster.

Upvotes: 0

Related Questions