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.

Community
  • 1
  • 1
Ivri
  • 2,159
  • 5
  • 25
  • 33
  • 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. – paprika Oct 05 '10 at 04:42
  • I've extracted data using that string, right now I want to extract data from it, just remove tags. – Ivri Oct 05 '10 at 04:49
  • 1
    http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 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. – msw Oct 05 '10 at 05:07

2 Answers2

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.

PaulMcG
  • 62,419
  • 16
  • 94
  • 130
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!

paprika
  • 2,424
  • 26
  • 46
  • Yes, that's what I did first, but I was looking for better solution – Ivri Oct 05 '10 at 08:56
  • 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). – paprika Oct 05 '10 at 10:23