简体   繁体   English

匹配不以字符p结尾的字符串

[英]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 . 我正在尝试编写一个正则表达式来匹配包含四个连续数字而不是字符p的字符串。

This is what I have: \\d\\d\\d\\d 这就是我所拥有的: \\d\\d\\d\\d

But I don't want the regex to match strings such as 1111p . 但是我不希望正则表达式匹配1111p字符串。 How can I improve my regex? 如何改善我的正则表达式?

You need to lookahead negative for presence of p ie absence of p 您需要lookahead判断是否存在p即不存在p

Regex: \\d{4}(?!p) 正则表达式: \\d{4}(?!p)

Regex101 Demo Regex101演示

/(\\d{4})(?!p)/ will capture all groups of four digits that aren't followed by the character p . /(\\d{4})(?!p)/将捕获字符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” . {}是一个量词 ,允许您指定最小,最大或确切的重复数,而(?!)是一个特殊的非捕获组,称为“负超前”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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