我想创建一个与baking.asp nhdjdl.asp hdghgdh.asp hksks.asp匹配的正则表达式
但不是任何以http://开头的文件
除了这一串字,我不知道该怎么说,我怎么做呢?
发布于 2011-06-27 09:42:17
消极的先行断言。
>>> re.match('(?!http://).*\\.asp', 'http://foo.asp')
>>> re.match('(?!http://).*\\.asp', 'foo.asp')
<_sre.SRE_Match object at 0x7f34f8432920>发布于 2011-06-27 09:53:15
如果您还希望排除以ftp://,等开头的字符串:
>>> re.match('[^/]*\.asp', '/tmp/foo.asp')
>>> re.match('[^/]*\.asp', 'http://foo.asp')
>>> re.match('[^/]*\.asp', 'ftp://foo.asp')
>>> re.match('[^/]*\.asp', 'foo.asp')
<_sre.SRE_Match object at 0x2abe856856b0>发布于 2011-06-27 10:05:49
我认为最好的解决方案是找到与“*.asp”匹配的所有模式,然后丢弃所有以"http:“开头的结果。由于您不了解正则表达式(假设您的队友也不了解,如果您有正则表达式的话),非regexp解决方案将是最清晰的。
例如:
[s for s in list_of_strings if s.endswith(".asp") and not s.startswith("http://")]https://stackoverflow.com/questions/6487889
复制相似问题