【发布时间】:2017-09-27 05:07:52
【问题描述】:
我正在尝试仅提取包含格式为
的数据的日志的 IP 地址和 URL 部分153.12.123.123 - - [13/Nov/2014:15:06:43 -0700] "GET /icons/AHPS/0.06.png HTTP/1.1" 123 1234 "http://198.123.123.123/index.html" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/1234567 Firefox/33.0"
153.12.123.123 - - [13/Nov/2014:15:06:43 -0700] "GET /icons/AHPS/0.06.png HTTP/1.1" 123 1234 "http://abc.weatherabc.org/?Center=38.123456789" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/1234556 Firefox/33.0"
我目前在命令行上使用这个表达式:
[^\"]*\"[^\"]*\"[^\"]*\"([^\"]*)\"
它会产生这些结果:
http://198.123.123.123/index.html
http://abc.weatherabc.org/?Center=38.123456789
但是我想要一个只产生这些部分的正则表达式:
http://198.123.123.123/
http://abc.weatherabc.org/
或
http://198.123.123.123
http://abc.weatherabc.org
请帮忙。提前致谢!
【问题讨论】:
-
那么,您的问题实际上是关于从 URL 中删除部分?
-
你不能用这个吗?
http:\/\/[0-9a-zA-z\.]+ -
(?<=\")http[^\"]+(?=/)怎么样? Link -
@David 也需要包含连字符
-
@David 成功了,谢谢!
标签: python regex python-2.7 sys