Repeating regex groups
I'm trying to get some information from a web site. The information I want is in a table so I made a regex but I don't know the right way to simplify it.
The following are two parts of my regex that I would like to simplify:
<br>(.*)<br>(.*)<br>(.*)
<tr><td>(.+)r>(.+)r>(.+)r>(.+).+</td></tr> # This part should be repeated n times(n = 1 to 10)
I looked through the python d开发者_StackOverflow中文版ocumentation and I can't realize how to do it. Perhaps you can give me a hint.
Thank you, mF.
This is the wrong way to go unless you're trying to scrape some data out of a tiny fragment.
It would be much better if you used a tolerant HTML. BeautifulSoup mentioned earlier is a good one but it's stagnating and I don't believe it's being maintained actively anymore.
A highly recommended parser for Python is lxml.
There was a long thread discussing parsing XHTML on one of our local mailing lists here which you might find useful too.
RegEx match open tags except XHTML self-contained tags
"Have you tried using an XML parser instead?"
EDIT: This is the way to go: Beautiful Soup
You just need to put the block in parens and then use the {...} operators, e.g.:
(foo...){1,10}
Matches 1 to 10 instances of the thing inside of there. Given your example above, you can nest those:
((f..)(b..)){1,10}
精彩评论