正则表达式匹配发生

时间:2016-01-08 18:01:13

标签: regex

我想在任何地方匹配一个包含2个字母p的文字。

那么应该匹配什么

pp
xppm
xpxpm
ppm

和什么不匹配

ppp
xpxpxp
mpphellop

1 个答案:

答案 0 :(得分:1)

使用此正则表达式:

^[^p]*p[^p]*p[^p]*$

Regex live here.

解释

^             # from start
[^p]*         # non 'p' characters from 0 to unlimited
p             # the first 'p' character
[^p]*         # non 'p' characters from 0 to unlimited
p             # the second 'p' character
[^p]*         # non 'p' characters from 0 to unlimited
$             # till the end

希望它有所帮助。