【发布时间】:2017-09-25 22:17:55
【问题描述】:
我有一个字符串输出,我需要从中解析一个特定的文件名:
>>> a = "Warning: do not enter your password if anyone else has superuser privileges or access to your account. [1] 15:04:16 [SUCCESS] 1.1.1.1 abc330b125.tar.bz2 my-libs.tar.bz2 xyz-notok-0.tar.gz Stderr: Could not create directory '/usr/share/httpd/.ssh'. Failed to add the host to the list of known hosts (/usr/share/httpd/.ssh/known_hosts)."
我试过这个,但我没有得到abc330b125.tar.bz2,而是得到bs.tar.bz2:
>>> re.findall(r'.*([abc|xyz\-ok|!my].*.tar.bz2)', a)
['bs.tar.bz2']
如果我在这里犯了任何错误,有人可以告诉我吗?
【问题讨论】:
-
方括号创建的字符集将仅匹配其中包含的一个字符。模式
[abc|xyz\-ok|!my]匹配 a、b、c、k、m、o、x、y、z、-、! 或 |恰好一次。
标签: python regex python-2.7