简体   繁体   中英

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?

You need to lookahead negative for presence of p ie absence of p

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

Regex101 Demo

/(\\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” .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM