2012-05-24 29 views
2

我試圖主機從這個文件中分離出來,但我的正則表達式同時選擇主機一起:正則表達式匹配的流動,因爲在一起*

timestamps|||scan_start|Tue May 1 23:00:29 2012|timestamps||foo.com|host_start|Tue May 1  23:16:51 2012|results|-0017\ntimestamps||foo.com|host_end|Tue May 1 23:19:17  2012|timestamps||bar.com|host_start|Tue May 1 23:24:31 2012|results|general/tcp|Sendmail 8.13.8\n\n\ntimestamps||bar.com|host_end|Tue May 1 23:29:11 2012|timestamps|||scan_end|Wed May 2 00:19:40 2012| 

正則表達式:

timestamps\|\|[\w,\.]*\|host_start.*host_end 

回答

3

讓明星懶惰:

timestamps\|\|[\w,.]*\|host_start.*?host_end 

.*是 「貪婪」,匹配儘可能多的,因爲它可以。 .*?是「懶惰」,並儘可能少地匹配以實現匹配。因此它只會匹配到最接近的host_end而不是最後一個。

另外,不需要轉義字符類中的點。你真的想在角色課中留一個逗號,還是你的意思是[\w.]*