Alaa Salah
Alaa Salah

Reputation: 1067

QRegExp - How to get specific text between two HTML tags

I want to extract some text from the following HTML code in "Qt/C++" using QRegExp.

<TABLE border="0" width="99%">
  <COLGROUP><COL width="100"><COL>
  <TR>
    <TD align="center" valign="middle" bgcolor="#EEEEFF"><B> MCT-to-KR</B>
    <TD align="center" valign="middle" bgcolor="#EEEEFF"><FONT class="result">THIS IS THE TEXT WHICH I WANT TO FETCH</FONT>
  <TR>
    <TD align="center" valign="middle" bgcolor="#DDDDEE">Normalized
    <TD align="center" valign="middle" bgcolor="#DDDDEE">[AL-LYA-A]
  <TR>
    <TD align="center" valign="middle" bgcolor="#CCCCDD">Web Unicode
    <TD align="center" valign="middle" bgcolor="#CCCCDD">ANOTHER TEXT I MIGHT BE INTERESTED IN, BUT WOULD BE EASY TO GET IF THE FIRST IS DONE
</TABLE>

In C#.Net I could do it like this:

private Regex txtExtractor = new Regex(@"<FONT class=""result"">(.*?)</FONT>");

I tired it with QRegExp but didn't work, so how should I edit it to make it work?

Upvotes: 0

Views: 1865

Answers (1)

RA.
RA.

Reputation: 7777

Using QRegEx (Qt 4 and Qt 5)

QRegExp regex("<FONT class=\"result\">(.*)</FONT>");
regex.indexIn(html);
QString textYouWant = regex.cap(1);

Using QRegularExpression (Qt 5)

QRegularExpression regex("<FONT class=\"result\">(.*)</FONT>");
QRegularExpressionMatch match = regex.match(html);
QString textYouWant = match.captured(1);

I recommend using QRegularExpression if you're using Qt 5. It's API is a bit nicer.

Upvotes: 2

Related Questions