列表中的正则表达式搜索数字有3个或更多数字

时间:2015-06-10 13:11:46

标签: regex

我有[0-9]中的6位数字列表。

示例:

001601
001602
001610
145784
487457
894571
111299
121118 

结果:

001601
001602
001610 zero occurs 3 times
111299 one occurs 3 times 
121118 one occurs 4 times 

2 个答案:

答案 0 :(得分:1)

这是一种perl方式:

my $re = qr~(.).*\1.*\1~;
while(<DATA>) {
    chomp;
    say $_ =~ $re ? "OK : $_" : "KO : $_";
}

__DATA__
001601
001602
001610
145784
487457
894571
111299
121118

<强>输出:

OK : 001601
OK : 001602
OK : 001610
KO : 145784
KO : 487457
KO : 894571
OK : 111299
OK : 121118

答案 1 :(得分:0)

我用php做了一个尝试 - &gt; 见Demo

代码:

$tada = array("001601", "001602", "001610", "145784", 
           "487457", "894571", "111299", "121118"); 

foreach($tada as $row)
{

$nb_occur_str = 0;
$value_occur = 0;
for($i = 0; $i < 10; ++$i)
{
    //echo $i . " is present " . substr_count($row, $i) . "times\n";
    if($nb_occur_str < substr_count($row, $i))
    {
        $nb_occur_str = substr_count($row, $i);
        $value_occur = $i;
    }
}
echo "In \"" . $row . "\" " . $value_occur . " occurs " . $nb_occur_str . " times\n";  
}

输出

In "001601" 0 occurs 3 times
In "001602" 0 occurs 3 times
In "001610" 0 occurs 3 times
In "145784" 4 occurs 2 times
In "487457" 4 occurs 2 times
In "894571" 1 occurs 1 times
In "111299" 1 occurs 3 times
In "121118" 1 occurs 4 times