Regex 匹配一些字符串

Regex 匹配一些字符串,regex,Regex,我想做一个正则表达式来匹配 烘焙.asp nhdjdl.asp hdghgdh.asp hksks.asp 但是没有任何以http://like开头的文件 既然除了这一系列的字符,我不知道该怎么说,我该怎么说呢 >>> re.match('(?!http://).*\\.asp', 'http://foo.asp') >>> re.match('(?!http://).*\\.asp', 'foo.asp') <_sre.SRE_Match objec

我想做一个正则表达式来匹配 烘焙.asp nhdjdl.asp hdghgdh.asp hksks.asp

但是没有任何以http://like开头的文件


既然除了这一系列的字符,我不知道该怎么说,我该怎么说呢

>>> re.match('(?!http://).*\\.asp', 'http://foo.asp')
>>> re.match('(?!http://).*\\.asp', 'foo.asp')
<_sre.SRE_Match object at 0x7f34f8432920>
>>re.match('(?!http://).\\\.asp','http://foo.asp')
>>>re.match('(!http://).\\\.asp',foo.asp')

消极的前瞻断言

>>> re.match('(?!http://).*\\.asp', 'http://foo.asp')
>>> re.match('(?!http://).*\\.asp', 'foo.asp')
<_sre.SRE_Match object at 0x7f34f8432920>
>>re.match('(?!http://).\\\.asp','http://foo.asp')
>>>re.match('(!http://).\\\.asp',foo.asp')

如果您还希望排除以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>
>>re.match('[^/]*\.asp','/tmp/foo.asp'))
>>>重新匹配('[^/]*\.asp','http://foo.asp')
>>>重新匹配('[^/]*\.asp','ftp://foo.asp')
>>>re.match('[^/]*\.asp',foo.asp')

如果您还希望排除以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>
>>re.match('[^/]*\.asp','/tmp/foo.asp'))
>>>重新匹配('[^/]*\.asp','http://foo.asp')
>>>重新匹配('[^/]*\.asp','ftp://foo.asp')
>>>re.match('[^/]*\.asp',foo.asp')

我认为最好的解决方案是找到与“*.asp”匹配的所有模式,并从中抛出以“http:”开头的任何结果。因为你不知道正则表达式(如果你有,你的队友可能也不知道),所以非regexp解决方案将是最清楚的

例如:

[s for s in list_of_strings if s.endswith(".asp") and not s.startswith("http://")]

我认为最好的解决方案是找到所有匹配“*.asp”的模式,并从中抛出任何以“http:”开头的结果。因为您不知道正则表达式(如果您有,您的队友可能也不知道),所以非regexp解决方案将是最清楚的

例如:

[s for s in list_of_strings if s.endswith(".asp") and not s.startswith("http://")]

@用户651521:没有。如您所见,第一个示例不匹配任何内容(因为提示符显示在下一行)。第二个匹配“foo.asp”。@user651521:它不匹配。如您所见,第一个示例不匹配任何内容(因为提示符显示在下一行)。第二个匹配“foo.asp”。