regex question
Helmut Jarausch
jarausch at skynet.be
Sat Jun 25 11:17:36 EDT 2005
Felix Schwarz wrote:
> Hi all,
>
> I'm experiencing problems with a regular expression and I can't figure
> out which words I use when googling. I read the python documentation for
> the re module multiple times now but still no idea what I'm doing wrong.
>
> What I want to do:
> - Extract all digits (\d) in a string.
> - Digits are separated by space (\w)
>
> What my program does:
> - It extracts only the last digit.
>
> Here is my program:
> import re
> line = ' 1 2 3'
> regex = '^' + '(?:\s+(\d))*' + '$'
> match = re.match(regex, line)
> print "lastindex is: ",match.lastindex
> print "matches: ",match.group(1)
>
>
> Obviously I do not understand how (?:\s+(\d))* works in conjunction with
> ^ and $.
>
I am sure what you like to do.
What about
regex= re.compile('\s+\d')
print regex.findall(line)
--
Helmut Jarausch
Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
More information about the Python-list
mailing list