0

I have a string like below, and I want to get both the two number "28" and "1", but right now I can only get "28" by my code. Please help me with this.

import re
content="""<span class="lineNum">      28 </span><span class="lineCov">          1 : get_pid_file(const char *file, pid_t *pid)</span>"""
pattern = "(\d+)"
ret = re.search(pattern,content)
if ret:
   print "find: %s" % ret.group()

2 Answers 2

1

Use re.findall,

>>> re.findall(r"\d+", content)
['28', '1']

But you may want to narrow down your regex.

EDIT:

You may want to change your regular expression to some variation of r"<span.*?>.*?(\d+).*?</span>" to match only numbers inside span tags.

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

1 Comment

Thanks for your help, how to narrow down my regex, because maybe there will be number exist in other place, so i only want to get the number between <span> </span> and the number before :
0
pattern = "(\d+).*(\d+)"
ret = re.search(pattern,content)
print ret.group(1), ret.group(2)

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.