简体   繁体   English

Perl:最大的正则表达式?

[英]Perl: A maximum in regular expressions?

Here's my code.... 这是我的代码。

#!/usr/bin/perl

$str = 'yaeeeeeeeeeeeeeeeeeeeeah';
$six = 6;
$eight = 8;

if( $str =~ /e{$six,$eight}?/)
{
 print "matches";
}

For some reason this still matches even though the number of e's exceeds the maximum 8. How can I make with regex return false if there are over 8 e's? 由于某些原因,即使e的数量超过了最大值8,这仍然匹配。如果e的数量超过8,如何用正则表达式返回false?

Generally its /(?<!e)e{$six,$eight}(?!e)/ 通常它的/(?<!e)e{$six,$eight}(?!e)/

Check http://www.perlmonks.org/?node_id=518444 检查http://www.perlmonks.org/?node_id=518444

For the really bad case where in the same string, 6-8 e's exist somewhere, but somewhere 对于同一字符串中6-8 e确实存在的非常糟糕的情况,
else, separately, 20 e's exist, the solution posted won't help. 否则,单独存在20个e,发布的解决方案将无济于事。

Example: rrrrrrrreeeeeeerrrrrrrrrrreeeeeeeeeeeeeee 示例: rrrrrrrreeeeeeerrrrrrrrrrreeeeeeeeeeeeeee

In that case you have to look way ahead for the bad case first e{9} , 在这种情况下,你必须遥遥领先查找不好的情况下第一e{9}
then the good case e{6,8} . 那么好的情况e{6,8}

/^(?!.*e{$nine}).*(?<!e)e{$six,$eight}(?!e)/

Your string matches the expressions, because it contains six e's. 您的字符串与表达式匹配,因为它包含六个e。 If you don't want to match, change the expression. 如果您不想匹配,请更改表达式。 For example, you can say that the sequence of e's is not preceded and followed by another e: 例如,您可以说e的序列不在另一个e的前面和后面:

/(?<!e) e{$six,$eight} (?!e)/x

These are called negative look-behind and look-ahead assertions. 这些称为否定的前瞻性断言和前瞻性断言。

The question mark after the quantifier makes no difference in such a case. 在这种情况下,量词后面的问号没有任何区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM