[Tutor] help with regular expressions

Bob Gailer bgailer at alum.rpi.edu
Wed Jun 30 08:20:20 EDT 2004


At 10:29 PM 6/29/2004, Jeff Peery wrote:
>hello I am having trouble with using the re module. I have a statement:
>
>line = FileHandle.readline(-1)
>test = findall('\d', line)
>
>where "line" is a line of text from a file I would like to read and 
>extract numbers from. the line is simply three numbers, example 
>3.444456    4   84.3546354.  I want to put these number in "test" but the 
>findall expression seems to only take whole numbers, so for example test 
>would be for the above numbers [3, 4, 4, 4, 4, 5, 6, 4, 8, ....].  How is 
>this done so that test = [3.444456    4   84.3546354]?

 >>> re.findall(r'\d+.?\d*', '3.444456    4   84.3546354')
['3.444456', '4 ', '84.3546354']

\d+ -> one or more digits
.? -> zero or 1 period
\d* -> zero or more digits

Bob Gailer
bgailer at alum.rpi.edu
303 442 2625 home
720 938 2625 cell 




More information about the Tutor mailing list