How can I check for aaa or aab or a in a Perl string?
Can 开发者_Go百科anybody tell me what pattern do I need to write in the bind expression if I want to check whether the string contains aaa or aab or a?
$str = "Hello how are aaa you?";
print $str =~ m/what should i write here?/;
$str =~ /a|aaa|aab/
You might want to check the regular operator precedence to make sure you don’t get caught by things like /^foo|bar$/
(matches ^foo
or bar$
, contrast with ^(foo|bar)$
).
$str =~ /a/
(Think about it...)
(aaa|aab|a)
...
Given mscha's point (answer not comment) I wonder if you really want to match and keep. If you want to know what you found try something like a global match captured to an array.
#!/usr/bin/perl
use strict;
use warnings;
my $str = "Hello how are aaa you?";
my @found = $str =~ m/(aaa|aab|a)/g;
foreach my $result (@found) {
print "found $result\n";
}
精彩评论