0
在PERL中使用正則表達式過濾包含的所有行的最佳方式是什麼?例如/usr/libexec/postfix
?包含某些目錄路徑的PERL正則表達式匹配行
那也會趕上,例如:/usr/libexec/postfix/qmgr
,/usr/libexec/postfix/smtp
,/usr/libexec/postfix/local
等等?
在PERL中使用正則表達式過濾包含的所有行的最佳方式是什麼?例如/usr/libexec/postfix
?包含某些目錄路徑的PERL正則表達式匹配行
那也會趕上,例如:/usr/libexec/postfix/qmgr
,/usr/libexec/postfix/smtp
,/usr/libexec/postfix/local
等等?
這應該工作:
if ($line =~ m{^/usr/libexec/postfix.*}) {
print "Match!\n";
} else {
print "No match\n";
}
工作實施例: http://codepad.org/jkVlISdv
的''是在這種情況下 – Zaid 2013-03-18 07:02:22
'多餘*'在'米{^的/ usr /的libexec /後綴。 *}'不是必要的,它可能會減慢匹配? – pynexj 2013-03-18 07:04:40
是的。但是當你需要像這樣將它捕獲到不同的組中時,它會派上用場:'my($ path,$ suffix)=($ line =〜m {^(/ usr/libexec/postfix)(。*)}); '。我不認爲你能夠測量單線速度差異。 – mvp 2013-03-18 07:06:25