为什么Perl正则表达式不匹配此字符串?

时间:2012-02-26 20:12:04

标签: regex perl

我在CSV文件中有这一行:

[2/16/2012] emailed...I honestly do not know - I am an endpoint in sales – I would try contacting our corporate office. <STOP>

这个Perl正则表达式:

m/\[(\d+\/\d+\/\d+)\]\s(.*)/

我希望这个正则表达式匹配上面的字符串。当我取出字符串中的“ - ”字符时,正则表达式匹配。否则,它没有。为什么?我以为“。”字符是指除换行符之外的任何字符?我做错了什么?

1 个答案:

答案 0 :(得分:0)

你的正则表达式看起来很好。我测试了下面的单行程,它成功捕获了两个字符串。

$ perl -e 'my $str = "[2/16/2012] emailed...I honestly do not know - I am an endpoint in sales – I would try contacting our corporate office. <STOP>"; print "full str: $str\n"; if ($str =~ m/\[(\d+\/\d+\/\d+)\]\s(.*)/) { print "matched\ndate: $1\nmsg: $2\n"; } else { print "did not match\n"; }'

full str: [2/16/2012] emailed...I honestly do not know - I am an endpoint in sales – I would try contacting our corporate office. <STOP>
matched
date: 2/16/2012
msg: emailed...I honestly do not know - I am an endpoint in sales – I would try contacting
our corporate office. <STOP>

上述单行内容更易于阅读:

my $str = "[2/16/2012] emailed...I honestly do not know - I am an endpoint in sales – I would try contacting our corporate office. <STOP>"; 
print "full str: $str\n"; 
if ($str =~ m/\[(\d+\/\d+\/\d+)\]\s(.*)/) {
    print "matched\ndate: $1\nmsg: $2\n";
} 
else { 
    print "did not match\n"; 
}

检查以确保字符串中没有隐藏的元字符。如果在linux上,你可以运行dos2unix来删除任何Windows添加的回车。如果在Windows上,您可以使用记事本++来显示所有字符。查看 - &gt;显示所有字符