2009-10-28 117 views
2

如何找到同一字符的多次出现? 类似:使用preg_match匹配多次出现的相同字符

$maxRepeat = 3; 

"pool" passes 

"poool" don't 

我需要这对任何字符工作,所以我想我得逃跑的特殊字符等。和\

哪些字符我必须逃脱?

你知道从php.net的任何一个很好的参考preg_match正则表达式分开?

回答

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是相当不错的。

+0

我认为他想的正则表达式的任何字符串,而不只是集中,而不仅仅是字母o重复 - 比如'$字符串=“aaaaany词”;' – NickSoft 2013-09-22 13:21:16

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

任何字符最多重复这么多次,你必须格式化你的正则表达式,如果你的$amxRepeate是一个int。

+0

点将匹配任何字符,而不是重复相同的字符。所以“oo”会匹配,但“ao”也匹配 – NickSoft 2013-09-22 13:22:55

1

发现,我需要的是

如果(的preg_match( '/()\ 1 /',$ T))返回true;

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

如果(的preg_match( '/()\ 1 \ 1 /。',$ t))的返回true;

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