应该找到第一个hello
,打印字符位置...找到下一个hello
并打印字符位置...并且锚可以是具有第一个hello
的任何行...
为什么不管用?
尝试#1:
$line = "\n hi\n hiya \n hello\n hi \n hello2";
$match = $line =~ m/^\s*(hello)/;
if (!$match) {
die("not found\n");
}
print "found at pos: " . pos($line) . "\n";
$line = $';
$match = $line =~ m/^\s*(hello)/;
if (!$match) {
die("not found\n");
}
print "found at pos: " . pos($line) . "\n";
结果:not found
尝试#2:
$line = "\n hi\n hiya \n hello\n hi \n hello2";
$match = $line =~ m/\A\s*(hello)/;
if (!$match) {
die("not found\n");
}
$line = $';
$match = $line =~ m/\A\s*(hello)/;
if (!$match) {
die("not found\n");
}
print "found at pos: " . pos($line) . "\n";
结果:not found
1条答案
按热度按时间kokeuurv1#
对于“多行”字符串,需要
/m
修饰符,以便^
匹配字符串内的行开头打印