[英]How do I match a .log but not .log* in regex or python?
I am having trouble with regular expressions. 我在使用正则表达式时遇到麻烦。
I have: 我有:
urls = re.findall(r'href=[\'"]?([^\'" >]+)', line)
print urls
which gives me: 这给了我:
['production_r1499.log']
['production_r1499.log-20140323']
['production_r1499.log-20140323.gz']
I am only interested in the .log file. 我只对.log文件感兴趣。 How do I get the regex to only match this one?
我怎样才能使正则表达式仅与之匹配?
alternatively. 或者。 Could some approach similar to this work?
某些方法可以类似于这项工作吗?
if str(urls).endswith('.log'):
Happy and grateful for suggestions! 高兴并感谢您的建议!
Try this. 尝试这个。
urls = re.findall(r'href=[\'"]?([^\'" >]+\.log)', line)
Strictly speaking, this should be anchored to the end of the href attribute. 严格来说,这应该锚定在href属性的末尾。 If you are concerned about false positives, maybe add something like
[\\'">]
before the closing quote. 如果您担心误报,可以在结束报价前添加
[\\'">]
之类的内容。
使用前瞻功能查看匹配中.log
后面是否有"
, '
"
, '
>
或space
。
urls = re.findall(r'href=[\'"]?([^\'" >]+\.log)(?=[\'"> ])', line)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.