问题描述
我正在学习正则表达式,我在这里有点困惑。我有一个字符串,我想从中提取一个至少4位且最多7位的int。我尝试了如下:
I am just learning regex and I'm a bit confused here. I've got a string from which I want to extract an int with at least 4 digits and at most 7 digits. I tried it as follows:
>>> import re
>>> teststring = 'abcd123efg123456'
>>> re.match(r"[0-9]{4,7}$", teststring)
在那里我期待123456,不幸的是,这根本没有任何结果。有人可以帮我一点吗?
Where I was expecting 123456, unfortunately this results in nothing at all. Could anybody help me out a little bit here?
推荐答案
@ExplosionPills是正确的,但你的正则表达式仍有两个问题。
@ExplosionPills is correct, but there would still be two problems with your regex.
首先, $
匹配字符串的 end 。我猜你也希望能够在字符串中间提取一个int,例如 abcd123456efg789
返回 123456
。要解决这个问题,你需要这个:
First, $
matches the end of the string. I'm guessing you'd like to be able to extract an int in the middle of the string as well, e.g. abcd123456efg789
to return 123456
. To fix that, you want this:
r"[0-9]{4,7}(?![0-9])"
^^^^^^^^^
增加的部分是一个否定的先行断言,意思是......后面没有更多的数字。让我通过使用 \d
来简化:但是:
The added portion is a negative lookahead assertion, meaning, "...not followed by any more numbers." Let me simplify that by the use of \d
though:
r"\d{4,7}(?!\d)"
这样更好。现在,第二个问题。你的正则表达式的左边没有约束,所以给定一个像 abcd123efg123456789
的字符串,你实际上匹配 3456789
。所以,你还需要一个否定的lookbehind断言:
That's better. Now, the second problem. You have no constraint on the left side of your regex, so given a string like abcd123efg123456789
, you'd actually match 3456789
. So, you need a negative lookbehind assertion as well:
r"(?<!\d)\d{4,7}(?!\d)"
这篇关于用于int的Python正则表达式至少有4位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!