简体   繁体   English

正则表达式:一个字符在一个序列中不能出现超过 4 次

[英]Regular expressions: a character cannot show more than 4 times in a sequence

I am creating a regular expression where the string MUST start with E, can have unlimited E's (E+), and MUST end with C ([C]).我正在创建一个正则表达式,其中字符串必须以 E 开头,可以有无限的 E (E+),并且必须以 C ([C]) 结尾。

Then B can OPTIONALLY be between E and C, but only in pairs (B{2}?).那么 B 可以选择在 E 和 C 之间,但只能成对出现(B{2}?)。

Now this is the part I'm stuck on.现在这是我坚持的部分。 There cannot be more than 4 P's in the string.字符串中的 P 不能超过 4 个。 I am trying to use S{0,4}.我正在尝试使用 S{0,4}。

Lastly, each P or sequence of P's in the series MUST be followed by a D.最后,系列中的每个 P 或 P 序列必须后跟一个 D。

I've been experimenting a lot but can't figure out the P and D.我一直在尝试很多,但无法弄清楚 P 和 D。

You can use您可以使用

^E+(?!(?:[^P\n]*P){5})(?:B{2}|P+D)*C$
  • (??(::[^P\n]*P){5}) - Make sure there are no more than 5 Ps on this line. (??(::[^P\n]*P){5}) - 确保这一行的 Ps 不超过 5 个。 Do this in a negative lookahead , rather than consuming the characters, to allow for more logic否定的前瞻方式执行此操作,而不是消耗字符,以允许更多逻辑

  • (?:B{2}|P+D)* - Every substring in the middle either needs to be a B pair, or some Ps followed by a D (?:B{2}|P+D)* - 中间的每个 substring 要么需要是 B 对,要么是一些 Ps 后跟 D

https://regex101.com/r/2tg7Mr/1 https://regex101.com/r/2tg7Mr/1

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

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