AJG519
AJG519

Reputation: 3379

Python Regular Expression Character following match does not equal

I am trying to match a string if it starts with '123' but only if the immediate following character is not a numeric.

For example, these would match:

But these would not match:

If I was only concerned with the string not ending with a numeric I could use:

'^123(?!.*\d$)'

However, this is NOT what I am looking for. I am looking for whether the character immediately following my match string is not a numeric.

Upvotes: 1

Views: 1610

Answers (2)

Ayush Seth
Ayush Seth

Reputation: 1209

anubhava's answer is correct, you can also use this which I think is simpler :

^123\D{1}

\D matches any non-digit character. you can replace {1} with any quantifier of your choice

Maybe you want to capture the full string, in that case you can modify :

^123\D{1}.*

Demo

Upvotes: 1

anubhava
anubhava

Reputation: 785156

I am looking for whether the character immediately following my match string is not a numeric.

You can use this regex:

^123(?!\d)

(?!\d) is negative lookahead that will assert failure if next character after 123 is a digit.

RegEx Demo

Upvotes: 1

Related Questions