如何将这 2 个正则表达式合并为一个正则表达式,该正则表达式根据字符串结构捕获所有可用部分( $s 中的最后 3 个字段是可选的,如果存在则应捕获)?使用 (?= ... ) 我无法获得有效的解决方案。
$s='1.2.3.4 - egon [10/Dec/2007:21:07:20 +0100] "GET /x.htm HTTP/1.1" 401 488';
$re = qr/\A
(\d+)\.(\d+)\.(\d+)\.(\d+)
[ ] (\S+)
[ ] (\S+)
[ ]+ \[(\d+)\/(\S+)\/(\d+):(\d+):(\d+):(\d+) [ ] (\S+)\]
[ ] "(\S+) [ ] (.*?) [ ] (\S+)"
[ ] (\S+)
[ ] (\S+)
\Z/x;
print "[".join('],[',$s =~ $re)."]\n\n";
$s='1.2.3.4 - - [13/Jun/2007:01:37:44 +0200] "GET /x.htm HTTP/1.0" 404 283 "-" "Mozilla/5.0..." "-"';
$re = qr/\A
(\d+)\.(\d+)\.(\d+)\.(\d+)
[ ] (\S+)
[ ] (\S+)
[ ]+ \[(\d+)\/(\S+)\/(\d+):(\d+):(\d+):(\d+) [ ] (\S+)\]
[ ] "(\S+) [ ] (.*?) [ ] (\S+)"
[ ] (\S+)
[ ] (\S+) [ ] "(.*?)" [ ] "(.*?)" [ ] "(.*?)"
\Z
/x;
print "[".join('],[',$s =~ $re)."]\n\n";