user6031489
user6031489

Reputation:

Match strings not ending with character p

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

Answers (2)

thomasd
thomasd

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

user2705585
user2705585

Reputation:

You need to lookahead negative for presence of p i.e absence of p

Regex: \d{4}(?!p)

Regex101 Demo

Upvotes: 2

Related Questions