rouven
rouven

Reputation: 13

Regex Lookahead and "String End" not working

I would like to match a String with a look-ahead using the following regex: /A20.(?!4)/. This string should match:

A20.1
A20.2
A20.3
A20.41
A20.42
A20.400
...

The only A20* string that should not match is

A20.4

It works fine, expect A20.41 or A20.42.. How can I terminate the regex? I have tried /A20.(?!4)$/, but it did not work.

Upvotes: 0

Views: 49

Answers (2)

Michał Turczyn
Michał Turczyn

Reputation: 37500

You could use negated character class such as [^4], which would mean "match everything except four". But I think you still want to match only digits, so I'd simply use character class [123567890] for that (note that 4 is excluded).

So pattern would be:

A20\.[123567890]

Also, you use . (dot) to match the dot, but dot is special regex character, so you need to escape it to treat it literally: \.

Upvotes: 1

Jakub Dóka
Jakub Dóka

Reputation: 2625

you have to look ahead more. If there is another digit behind the 4 then match it, so:

A20.((?!4)|(?=\d\d))

Upvotes: 0

Related Questions