Reputation: 61
How to use Regular Expression to extract the answer "Here is the answer" from a HTML webpage like this?
<b>Last Question:</b>
<b>Here is the answer</b>
Upvotes: 2
Views: 410
Reputation: 21728
<b>Last Question:</b>\\s*(<b>.*?</b>)
Or, in more details,
String x ="<b>Last Question:</b>\n<b>Here is the answer</b>";
Pattern p = Pattern.compile("<b>Last Question:</b>\\s*(<b>.*?</b>)");
Matcher m = p.matcher(x);
if (m.find())
System.out.println(m.group(1));
Regular expressions are still an option when HTML or similar tags are just not present or appear at random without providing enough context information on their own. In such cases, we need to look into some words of the human speech instead.
Upvotes: 0
Reputation: 61
Thanks everybody!
Here is my solution by using BeautifulSoup since I'm using Python framework:
response = opener.open(url)
the_page = response.read()
soup = BeautifulSoup(''.join(the_page))
paraText1 = soup.body.find('div', 'div_id', text = u'Last Question:')
if paraText1:
answer = paraText1.next
Upvotes: 1
Reputation: 3871
I know regex is not recommeded to parse html but to answer your question, if you are using php simplehtmldom is your friend. http://simplehtmldom.sourceforge.net/
Upvotes: 1
Reputation: 5853
As Charles said, don't use regex for this; if you're using PHP I'd recommend the DOM parsing functionality built in, coupled with the XPath methods proves quite reliable.
If you're more open than that I'd recommend using jQuery to do the job via Node.js, been doing that a lot lately myself- it makes life easy.
Upvotes: 0
Reputation: 1108537
Don't use regex. Use a HTML parser like Jsoup.
String html = "<b>Last Question:</b><b>Here is the answer</b>";
Document document = Jsoup.parse(html);
Element secondBold = document.select("b").get(1);
System.out.println(secondBold.text()); // Here is the answer
Jsoup is Java based. For other programming languages there are also HTML parsers available. If you're using C#, have a look at Nsoup. If you're using PHP, have a look at phpQuery (all of those parsers use jQuery-like CSS3 selectors to select elements, which is simply brilliant).
Upvotes: 0
Reputation: 11479
Don't use regexen to parse HTML. This goes double if instead of well-formed SGML/XML/HTML5 you have tag soup.
Upvotes: 0