tags:

views:

226

answers:

5

Hi there, I am having some trouble with Python giving me a result I do not expect. 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".

In the second block I don't even get an object, where I am expection the string "2".

While when I do this in bash everything looks fine :

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

Can somebody help me please?

+1  A: 

Is this what you're looking for?

>>> re.findall(r'([0-9])', "test test2 test_ 2 333")
['2', '2', '3', '3', '3']
Adam Bernier
That works perfect. And while I was waiting I kept working on it and discovered "re.finditer" which was working fine as well.By the way, I just realised that POSIX character classes are not supported by Python.
550
Glad you figured the rest out! The other answers are much more comprehensive. (I was unexpectedly pulled away and didn't get a chance to elaborate.)
Adam Bernier
In fact, for The Greater Good, I would ask that you accept Laurence Gonsalves' answer instead (if that's even possible): http://stackoverflow.com/questions/994178/python-regular-expression-with-numeric/994218#994218
Adam Bernier
+2  A: 

You are missing the () which capture the contents for use with the groups() (and other) function(s).

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

number = re.search(" ([[:digit:]]) ", "test test2 test_ 2 333")
print number.groups()
lambacck
+1  A: 
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).

Matthew Flaschen
A: 

.groups() returns values inside of matched parentheses. 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',)

@adam: great answer! tip: try highlighting your code and pressing ctrl-k to get your code to format properly, with syntax-highlighting.
Adam Bernier
+3  A: 

The groups() method returns the capture groups. It does not return group 0, in case that's what you were expecting. 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. Use \d. eg:

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