我在python 2.7.5中使用re
用于正则表达式。我想让它与foobar.com/1
,`foobar.com/12
,foobar.com/123
或foobar.com/1324
匹配,但不是 foobar.com/
或{{1 }}
我当前的正则表达式是foobar.com/12345
,但这只会匹配在所需字符串后面包含非字,非空格,非字符串结尾字符的字符串。
如何将字符串与除之外的字符串匹配为字母数字?
代码:
foobar\.com/\d\d?\d?\d?\W
输入:
pattern1 = re.compile("foobar\.com/\d\d?\d?\d?\W")
match = pattern1.search(comment.body)
print match
(由双新行分隔的字符串,字符串#3,4,7和9应匹配。)
输出:
foobar.com/12345
random text
[relevant](http://foobar.com/1319)
foobar.com/567
other comment
random comment
foobar.com/1302/
foobar.com
foobar.com/201
This is a test
You are looking at VI model 1.7 AGB Commander Shepard. Please see a store clerk to unlock a demo of this model.
Listen, if you don't have the credits just...tear me out of the terminal. Or somehting.
I sound seven percent more like Commander Shepard than any other bootleg VI copy.
SHEPHERDVI
SHEPARDVI
shepherdvi
You want help solving your problems? Get me out of this damn demo mode.
Shepard VI
Hey it works
Commander Shepard. Allicance Navy.
Commander Shepard. Allicance Navy.
TestShepard
TestShepard
Onelasttest
I sound seven percent more like Commander Shepard than any other bootleg VI copy.
答案 0 :(得分:2)
foobar\.com/\d{1,4}\b
会做的伎俩。
答案 1 :(得分:2)
...或者您可以使用否定前瞻(?!...)
来确保没有第五位数字。
>>> re.findall(r'foobar[.]com/\d{1,4}(?!\d)', comment.body)
['foobar.com/1319', 'foobar.com/567', 'foobar.com/1302', 'foobar.com/201']