5

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!

1
  • use the search method (for only one match) instead of findall (for several matches). You must put your pattern into a raw string r'555=\d+' or escape the backslashes '555=\\d+'. If you want to only extract the value on the right, use a capture group or, why not a lookbehind (but it's slower). Commented Aug 21, 2015 at 21:01

2 Answers 2

8

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

Sign up to request clarification or add additional context in comments.

Comments

0

You should capture the expression you want using "()". So your expression should be p = re.compile('555=(\d+)')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.