I have a pattern, Let it be this,
pppoe lg id 827 vlan-id 100 pppoe user@local local Jan 3 14:41:20
from this i wanted to grep for the pattern 'pppoe' and want to ensure that it is present exactly two times.
pppoe lg id 827 vlan-id 100 pppo user@local local Jan 3 14:41:20
here pppoe
is present only once, other is pppo
, this time it should not match.
A straight forward way is to put the matches in an array and check the number of items:
my $str = "pppoe lg id 827 vlan-id 100 pppoe user\@local local Jan 3 14:41:20";
my @count = $str =~ /pppoe/g;
if (scalar @count == 2) {
print "MATCH exactly two times\n";
}
or, as suggested by mpapec :
print "MATCH\n" if scalar @{[ $str =~ /pppoe/g ]} == 2;
All that is necessary is
if ($str =~ /pppoe.*pppoe/) {
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.