使用preg_match匹配多个相同字符的匹配项

时间:2009-10-28 15:18:07

标签: php regex preg-match

如何找到同一个字符的多个匹配项? 类似的东西:

$maxRepeat = 3;

"pool" passes

"poool" don't

我需要这个适用于任何角色,所以我想我必须逃避像这样的特殊角色。和\

我必须逃脱哪些角色?

除了php.net上的那个之外,你对preg_match正则表达式有什么好的参考吗?

3 个答案:

答案 0 :(得分:4)

您使用quantifiers作为此

preg_match("/p(o){1,3}ls/",$string);

摘录:

The following standard quantifiers are recognized:

1. * Match 0 or more times
2. + Match 1 or more times
3. ? Match 1 or 0 times
4. {n} Match exactly n times
5. {n,} Match at least n times
6. {n,m} Match at least n but not more than m times

我最喜欢的学习资源 P erl 注册 ular表达式是时间camel book。但如果你没有一个方便,this site就相当不错了。

答案 1 :(得分:1)

发现,我需要的是

if(preg_match('/(。)\ 1 /',$ t))返回true;

对于$ t ='aa',返回true; //任何字符

if(preg_match('/(。)\ 1 \ 1 /',$ t))返回true;

对于$ t ='aaa',返回true; //任何字符

等等

答案 2 :(得分:-1)

/.{1,2}/         # 2 is limit, 1 to have at least one character

任何重复多次的角色,如果你的$amxRepeate是int,你必须格式化你的正则表达式。