简体   繁体   English

一个非常简单的正则表达式的问题

[英]Trouble with a very simple regex

I am using python to try to write some simple code that looks through strings with regular expressions and finds things.我正在使用 python 尝试编写一些简单的代码,使用正则表达式查看字符串并查找内容。 In this string:在这个字符串中:

and the next nothing is 44827

I want my regex to return just the numbers.我希望我的正则表达式只返回数字。

I have set up my python program like this:我已经像这样设置了我的 python 程序:

buf = "and the next nothing is 44827"
number = re.search("[0-9]*", buf)
print buf
print number.group()

What number.group() returns is an empty string. number.group() 返回的是一个空字符串。 However, when the regex is:但是,当正则表达式为:

number = re.search("[0-9]+", buf)

The full number (44827) is properly extracted.完整的数字 (44827) 已正确提取。 What am I missing here?我在这里错过了什么?

The problem is that [0-9]* matches zero or more digits, so it is more than happy to match to a zero-length string.问题是[0-9]*匹配零个或多个数字,因此很乐意匹配零长度字符串。

Meanwhile, [0-9]+ matches one or more digits, so it needs to see at least one number in order to catch.同时, [0-9]+匹配一个或多个数字,因此它至少需要看到一个数字才能捕捉到。


you might want to use findall and handle the case in which you have multiple numbers per line.您可能想使用findall并处理每行有多个数字的情况。

Your first regex matches the empty string before the letter "a", so it stops there.您的第一个正则表达式匹配字母“a”之前的空字符串,因此它停在那里。 Your second doesn't, so it keeps trying.你的第二个没有,所以它一直在尝试。

It's because the first attempt matches an empty string - you're asking it for "0 or more digits" - so the first match is empty at the beginning of the string.这是因为第一次尝试匹配一个空字符串——你要求它输入“0 个或更多数字”——所以第一个匹配在字符串的开头是空的。 When you ask for "one or more digits", the first match starts at the first '4', and continues from there until the end of the number.当您要求“一个或多个数字”时,第一个匹配项从第一个“4”开始,并从那里继续直到数字结束。

See for yourself.你自己看。

Hint:暗示:

  • * matches 0-or-more times *匹配 0 次或多次
  • + matches 1-or-more times +匹配 1 次或多次

Obviously, the first case has more precedence over the second.显然,第一种情况比第二种情况更优先。 And the regex engine has NO problem at all, to not match anything.正则表达式引擎完全没有问题,不匹配任何东西。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM