Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to extract token from string in python?

Tags:

python

regex

I'm pretty new to Python and I know Perl can do this, but I haven't found it in Python yet. What I'm trying to do is to extract a token from a line.

p = re.compile('555=\d+')
ID = p.findall(line)

where line is a single line in a file, and the digits after 555= should be recorded. However, with these two lines, I can only get things like 555=1234567, what I really want is 1234567. Can anyone help and suggest a solution? Thanks!

like image 586
Sarah Avatar asked Aug 21 '15 20:08

Sarah


1 Answers

Use () to capture what you want:

>>> p = re.compile('555=(\d+)')
>>> p.findall("555=1234567")
['1234567']

(...)

Matches whatever regular expression is inside the parentheses, and indicates the start and end of a group; the contents of a group can be retrieved after a match has been performed, and can be matched later in the string with the \number special sequence

ref

like image 125
Assem Avatar answered Oct 01 '22 18:10

Assem