2016-01-08 298 views
-2

我試圖匹配任何地方包含正好包含2個字母p的文本。正則表達式匹配

那麼應該怎麼搭配

pp 
xppm 
xpxpm 
ppm 

,什麼不應該匹配

ppp 
xpxpxp 
mpphellop 
+2

你的問題是什麼?你有什麼嘗試? – Martin

回答

1

使用這個表達式:

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

Regex live here.

Explai寧:

^    # 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 

希望它有幫助。