Eric Hansen
Eric Hansen

Reputation: 1809

Matching certain string ending in 1(any digit) but not just 1

I'm trying to write a regex in Python to match strings such as rds2, rds5, rds11, but not match the string rds1 or any non rds-followed-by-digits string, e.g. camels2.

My current regex is just

^rds[^1]

however this doesn't match strings which end in 1 followed by another digit. I am not very familiar with regex, but I think I have to use a look-behind assertion to make sure that at the end of the string, if the last character is 1 then the previous character was [0-9]?

I attempted to do so, but have ended up in a mess where the look-behind assertion length seems variable on how many digits the string ends with.

Upvotes: 0

Views: 101

Answers (1)

Toto
Toto

Reputation: 91385

Use negative lookahead:

^rds(?!1$)\d+$

Upvotes: 1

Related Questions