3

我怎样才能找到同一个字符的多次出现?就像是:

$maxRepeat = 3;

"pool" passes

"poool" don't

我需要这个适用于任何角色,所以我想我必须转义特殊字符,如 . 和 \

我必须逃避哪些角色?

preg_match除了 php.net 上的 regexp 之外,您还知道有什么好的参考吗?

4

3 回答 3

4

你为此使用量词

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

我最喜欢的学习 Perl 正则表达式的资源久负盛名的骆驼书。但是,如果您手边没有一个,这个网站是相当不错的。

于 2009-10-28T15:21:33.543 回答
1

找到了,我需要的是

if(preg_match('/(.)\1/', $t)) 返回真;

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

if(preg_match('/(.)\1\1/', $t)) 返回真;

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

等等

于 2009-10-28T15:23:22.820 回答
-1
/.{1,2}/         # 2 is limit, 1 to have at least one character

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

于 2009-10-28T15:22:02.527 回答