Jerad Rose
Jerad Rose

Reputation: 15513

Regex for: Must contain a numeric, excluding "SomeText1"

I need a regex pattern (must be a single pattern) to match any text that contains a number, excluding a specific literal (i.e. "SomeText1").

I have the match any text containing a number part:

^.*[0-9]+.*$

But am having a problem excluding a specific literal.

Update: This is for .NET Regex.

Thanks in advance.

Upvotes: 1

Views: 514

Answers (2)

Tim Pietzcker
Tim Pietzcker

Reputation: 336328

As a verbose regex:

^              # Start of string
(?=.*[0-9])    # Assert presence of at least one digit
(?!SomeText1$) # Assert that the string is not "SomeText1"
.*             # If so, then match any characters
$              # until the end of the string

If your regex flavor doesn't support those:

^(?=.*[0-9])(?!SomeText1$).*$

Upvotes: 5

Andrey Adamovich
Andrey Adamovich

Reputation: 20663

Use negative-look-ahead:

^(?!.*?SomeText1).*?[0-9]+.*$

Upvotes: 0

Related Questions