Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python正则表达式查找空白、字符串结尾和/或单词边界_Python_Regex_Python 2.7 - Fatal编程技术网

Python正则表达式查找空白、字符串结尾和/或单词边界

Python正则表达式查找空白、字符串结尾和/或单词边界,python,regex,python-2.7,Python,Regex,Python 2.7,我在Python2.7.5中为正则表达式使用re。我试图让它匹配foobar.com/1,`foobar.com/12,foobar.com/123,或foobar.com/1324,但不是foobar.com/或foobar.com/12345 我当前的正则表达式是foobar\.com/\d\d?\d?\d?\W,但这将只匹配在所需字符串后具有非单词、非空白、非字符串结尾字符的字符串 如何使字符串与字母数字以外的任何字符匹配 代码: 输入: foobar.com/12345 random

我在Python2.7.5中为正则表达式使用
re
。我试图让它匹配
foobar.com/1
`foobar.com/12
foobar.com/123
,或
foobar.com/1324
,但不是
foobar.com/
foobar.com/12345

我当前的正则表达式是
foobar\.com/\d\d?\d?\d?\W
,但这将只匹配在所需字符串后具有非单词、非空白、非字符串结尾字符的字符串

如何使字符串与字母数字以外的任何字符匹配

代码:

输入:

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.
(由双新行分隔的字符串,字符串#3、4、7和9应匹配。)

输出:

None
None
<_sre.SRE_Match object at 0x103f1a578>
None
None
None
<_sre.SRE_Match object at 0x103f1a578>
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
无
没有一个
没有一个
没有一个
没有一个
没有一个
没有一个
没有一个
没有一个
没有一个
没有一个
没有一个
没有一个
没有一个
没有一个
没有一个
没有一个
没有一个
没有一个
没有一个
没有一个
没有一个
没有一个
我会成功的


会成功的。

。。。或者您可以使用负前瞻
(?!…)
来确保没有第五位数字

>>> re.findall(r'foobar[.]com/\d{1,4}(?!\d)', comment.body)
['foobar.com/1319', 'foobar.com/567', 'foobar.com/1302', 'foobar.com/201']

。。。或者您可以使用负前瞻
(?!…)
来确保没有第五位数字

>>> re.findall(r'foobar[.]com/\d{1,4}(?!\d)', comment.body)
['foobar.com/1319', 'foobar.com/567', 'foobar.com/1302', 'foobar.com/201']

foobar\.com/\d{1,4}\b
。请注意,您需要避开圆点
\b
是一个与单词边界处的空字符串相匹配的断言。
foobar\.com/\d{1,4}\b
。请注意,您需要避开圆点
\b
是一个与单词边界处的空字符串相匹配的断言。它似乎不匹配。使用
pattern.search()
我得到了0个匹配项,而使用问题中的正则表达式,我得到了4个匹配项中的2个desired@ZuluDeltaNiner,这没有帮助:编辑您的问题以准确显示您运行的代码、获得的输出和想要的输出。没有人能读懂你的心思;-)看来不是。使用
pattern.search()
我得到了0个匹配项,而使用问题中的正则表达式,我得到了4个匹配项中的2个desired@ZuluDeltaNiner,这没有帮助:编辑您的问题以准确显示您运行的代码、获得的输出和想要的输出。没有人能读懂你的心思;-)
>>> re.findall(r'foobar[.]com/\d{1,4}(?!\d)', comment.body)
['foobar.com/1319', 'foobar.com/567', 'foobar.com/1302', 'foobar.com/201']