2

I want to extract data from such regex:

<td>[a-zA-Z]+</td><td>[\d]+.[\d]+</td><td>[\d]+</td><td>[\d]+.[\d]+</td>  

I've found related question extract contents of regex but in my case I shoud iterate somehow.

3
  • 1
    Please clarify: are you trying to extract data from that string or by using that string (regex)? For the latter, you need to use parentheses to build groups, just like in the accepted answer of the question you linked to. Commented Oct 5, 2010 at 4:42
  • I've extracted data using that string, right now I want to extract data from it, just remove <td></td> tags. Commented Oct 5, 2010 at 4:49
  • 1
    stackoverflow.com/questions/1732348/… The StackOverflow software should just post this anytime "regex" and an HTML tag appear in the same post. Short version: regexps can't parse HTML. Commented Oct 5, 2010 at 5:07

2 Answers 2

7

As paprika mentioned in his/her comment, you need to identify the desired parts of any matched text using ()'s to set off the capture groups. To get the contents from within the td tags, change:

<td>[a-zA-Z]+</td><td>[\d]+.[\d]+</td><td>[\d]+</td><td>[\d]+.[\d]+</td> 

to:

<td>([a-zA-Z]+)</td><td>([\d]+.[\d]+)</td><td>([\d]+)</td><td>([\d]+.[\d]+)</td>
     ^^^^^^^^^           ^^^^^^^^^^^           ^^^^^           ^^^^^^^^^^^
      group 1             group 2              group 3          group 4

And then access the groups by number. (Just the first line, the line with the '^'s and the one naming the groups are just there to help you see the capture groups as specified by the parentheses.)

dataPattern = re.compile(r"<td>[a-zA-Z]+</td>... etc.")
match = dataPattern.find(htmlstring)
field1 = match.group(1)
field2 = match.group(2)

and so on. But you should know that using re's to crack HTML source is one of the paths toward madness. There are many potential surprises that will lurk in your input HTML, that are perfectly working HTML, but will easily defeat your re:

  • "<TD>" instead of "<td>"

  • spaces between tags, or between data and tags

  • "&nbsp;" spacing characters

Libraries like BeautifulSoup, lxml, or even pyparsing will make for more robust web scrapers.

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

Comments

0

As the poster clarified, the <td> tags should be removed from the string.

Note that the string you've shown us is just that: a string. Only if used in the context of regular expression functions is it a regular expression (a regexp object can be compiled from it).

You could remove the <td> tags as simply as this (assuming your string is stored in s): s.replace('<td>','').replace('</td>','')

Watch out for the gotchas however: this is really of limited use in the context of real HTML, just as others pointed out.

Further, you should be aware that whatever regular expression [string] is left, what you can parse with that is probably not what you want, i.e. it's not going to automatically match anything that it matched before without <td> tags!

2 Comments

Yes, that's what I did first, but I was looking for better solution
Better with respect to what? If all you do is removing stuff then such a simple solution is totally fine. Try to keep code as simple as possible (but not simpler).

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.