Reputation:
I'm trying to write a regex to match strings containing four consecutive digits not followed by the character p
.
This is what I have: \d\d\d\d
But I don't want the regex to match strings such as 1111p
. How can I improve my regex?
Upvotes: 1
Views: 51
Reputation: 2612
/(\d{4})(?!p)/
will capture all groups of four digits that aren’t followed by the character p
. The {}
is a quantifier that allows you to specify a minimum, maximum, or exact number of repetitions and the (?!)
is a special non-capturing group called a “negative lookahead”.
Upvotes: 0
Reputation:
You need to lookahead
negative for presence of p
i.e absence of p
Regex: \d{4}(?!p)
Upvotes: 2