简体   繁体   English

具有[:numeric:]的Python正则表达式

[英]Python regular expression with [:numeric:]

I am having some trouble with Python giving me a result I do not expect. 我在使用Python时遇到了麻烦,给了我意外的结果。 Here is a sample code : 这是一个示例代码:

number = re.search(" [0-9] ", "test test2 test_ 2 333")
print number.groups()

number = re.search(" [[:digit:]] ", "test test2 test_ 2 333")
print number.groups()

In the first block I get an object returned but with nothing in it. 在第一个块中,我得到一个返回的对象,但其中没有任何内容。 Where I think I should get the string "2". 我认为应该在其中获取字符串“ 2”。

In the second block I don't even get an object, where I am expection the string "2". 在第二个块中,我什至没有得到一个对象,我希望该字符串为“ 2”。

While when I do this in bash everything looks fine : 当我在bash中执行此操作时,一切看起来都很好:

echo "test test2 test_ 2 333" | grep " [[:digit:]] "
echo "test test2 test_ 2 333" | grep " [0-9] "

Can somebody help me please? 有人可以帮我吗?

The groups() method returns the capture groups. groups()方法返回捕获组。 It does not return group 0, in case that's what you were expecting. 返回0组,如果这是您所期望的东西。 Use parens to indicate capture groups. 使用括号表示捕获组。 eg: 例如:

>>> number = re.search(" ([0-9]) ", "test test2 test_ 2 333")
>>> print number.groups()
('2',)

For your second example, Python's re module doesn't recognize the "[:digit:]" syntax. 对于第二个示例,Python的re模块无法识别“ [:digit:]”语法。 Use \\d . 使用\\d eg: 例如:

>>> number = re.search(r" (\d) ", "test test2 test_ 2 333")
>>> print number.groups()
('2',)

You are missing the () which capture the contents for use with the groups() (and other) function(s). 您缺少()来捕获供groups()(和其他)函数使用的内容。

number = re.search(" ([0-9]) ", "test test2 test_ 2 333")
print number.groups()

This however won't work because python does not support the [[:number:]] notation 但是,这将不起作用,因为python不支持[[:number:]]表示法

number = re.search(" ([[:digit:]]) ", "test test2 test_ 2 333")
print number.groups()

Is this what you're looking for? 这是您要找的东西吗?

>>> re.findall(r'([0-9])', "test test2 test_ 2 333")
['2', '2', '3', '3', '3']
number = re.search(" [0-9] ", "test test2 test_ 2 333")
print number.group(0)

groups() only returns groups 1 and up (a bit odd if you're used to other languages). groups()仅返回第1组及以上的组(如果您习惯于其他语言,则有点奇怪)。

.groups() returns values inside of matched parentheses. .groups()返回匹配括号内的值。 This regex doesn't have any regions defined by parens so groups returns nothing. 此正则表达式没有任何由括号定义的区域,因此组不返回任何内容。 You want: 你要:

m = re.search(" ([0-9]) ", "test test2 test_ 2 333") m.groups() ('2',) m = re.search(“([0-9])”,“ test test2 test_23333”)m.groups()('2',)

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

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