本文介绍了Python Regex 查找空格、字符串结尾和/或单词边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

对于字母数字,我如何使它匹配带有任何字符的字符串?

代码:

pattern1 = re.compile("foobar\.com/\d\d?\d?\d?\W")匹配 = pattern1.search(comment.body)打印匹配

输入:

foobar.com/12345随机文本[相关](http://foobar.com/1319)foob​​ar.com/567其他评论随机评论foob​​ar.com/1302/foob​​ar.comfoob​​ar.com/201这是一个测试您正在查看 VI 模型 1.7 AGB Commander Shepard.请与店员联系以解锁此模型的演示.听着,如果你没有学分就……把我从终端里拉出来.或者什么.与任何其他盗版 VI 副本相比,我听起来更像薛帕德指挥官 7%.牧羊人谢帕尔维牧羊人您需要帮助解决您的问题吗?让我摆脱这个该死的演示模式.谢泼德六世嘿它有效谢泼德指挥官.联盟海军.谢泼德指挥官.联盟海军.测试谢泼德测试谢泼德最新测试与任何其他盗版 VI 副本相比,我听起来更像薛帕德指挥官 7%.

(用双换行分隔的字符串,字符串#3、4、7、9 应该匹配.)

输出:

无没有任何<_sre.SRE_Match 对象在 0x103f1a578>没有任何没有任何没有任何<_sre.SRE_Match 对象在 0x103f1a578>没有任何没有任何没有任何没有任何没有任何没有任何没有任何没有任何没有任何没有任何没有任何没有任何没有任何没有任何没有任何没有任何没有任何没有任何
解决方案

... 或者你可以使用否定前瞻 (?!...) 来确保没有第五位数字.

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

I am using re in python 2.7.5 for regex. I am trying to have it match foobar.com/1, `foobar.com/12, foobar.com/123, or foobar.com/1324, but not foobar.com/ or foobar.com/12345.

My current regex is foobar\.com/\d\d?\d?\d?\W, but this will only match strings that have a non-word, non-whitespace, non-end-of-string character after the desired string.

How do I make it match strings with any character except for an alpha-numeric?

Code:

pattern1 = re.compile("foobar\.com/\d\d?\d?\d?\W")
match = pattern1.search(comment.body)
print match

Input:

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.

(Strings separated by double new line, strings #3, 4, 7, and 9 should match.)

Output:

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
解决方案

... or you could use the negative lookahead (?!...) to make sure there is not a fifth digit.

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

这篇关于Python Regex 查找空格、字符串结尾和/或单词边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 21:38