Skip to content Skip to sidebar Skip to footer

Qt Regex Matches Html Tag Innertext

I have a html file with one
...
tag. What regex is necessary to match all content within the pre's? QString pattern = '
(.*)
'; QRegExp

Solution 1:

HTML is not a regular language, you do not use regular expressions to parse it.

Instead, use QXmlSimpleReader to load the XML, then QXmlQuery to find the PRE node and then extract its contents.


Solution 2:

DO NOT PARSE HTML USING Regular Expressions!

Instead, use a real HTML parser, such as this one


Solution 3:

i did it using substrings:

int begin = clipBoardData.indexOf("<pre");
int end = clipBoardData.indexOf("</body>");

QString result = data.mid(begin, end-begin);

The result includes the <pre's> but i found out thats even better ;)


Solution 4:

I have to agree with the others. Drupal 6.x and older are using regex to do a lot of work on the HTML data. It quickly breaks if you create pages of 64Kb or more. So using a DOM or just indexOf() as you've done is a better much faster solution.

Now, for those interested in knowing more about regex, Qt uses the perl implementation. This means you can use the lazy operator. Your regex would become:

(<pre>.*?</pre>)+

to get each one of the <pre> block in your code (although if you have only one, then the question mark and the plus are not required.) Note that no delimiters at the start and end of the regular expression are required here.

QRegExp re("(<pre>.*?</pre>)+", Qt::CaseInsensitive);
re.indexIn(html_input);
QStringList list = re.capturedTexts();

Now list should have one <pre> tag or more.


Post a Comment for "Qt Regex Matches Html Tag Innertext"