Rasmus Søborg
Rasmus Søborg

Reputation: 3695

Regex match if word does not end with

I am trying to make an algorithm for regex that finds uncalculated calculations such as '15+15', however; it should not match '15+15=30'

So far I have made it work to find calculations such as 15+15, however; it also matches 15+15=30

What I have got so far is

\d{1,9}\+\d{1,9}

I tried with

\d{1,9}\+\d{1,9}[^=]

But it didn't really work as I expected.

I am using the .net 'Regex' class

Upvotes: 1

Views: 943

Answers (1)

Martin Ender
Martin Ender

Reputation: 44259

What you need is a negative lookahead:

(\d{1,9}\+\d{1,9})\b(?!=)

This asserts that the pattern is not followed by =. The \b is a word boundary that makes sure that you don't match 15+1 in 15+15=30 (because 5 is not =).

The reason why your attempt with the negated character class doesn't work is that it needs a non-= character after the match.

Upvotes: 4

Related Questions