PDA

View Full Version : Regular Expression using QRegularExpression sometimes failed



jcdole
17th October 2013, 11:01
Hello.

Here is the code I use to extract a sub-string of unknown length. This character sub-string begins with the sequence code= and ends with a space.


QString town_code;
QRegularExpression re1("code=(.+)\\s");
QRegularExpressionMatch match = re1.match (a_text);
hasMatch = match.hasMatch();
if (hasMatch) {
town_code = match.captured(1);
............
............
............
}

The result is correct for variable a_text containing:
ARANTZAZU (OÑATI) code=onati-id20059 ville=OÑATI
The town_code variable contains the correct value: onati-id20059


The result is incorrect for variable a_text containing:
OTSAURTE (ZEGAMA) code=Zegama-id20025 ville=ZEGAMA tameteo=http://www.tameteo.com/meteo_Zegama-Europe-Espagne-Guipuscoa--1-3385.html
The town_code variable contains the incorrect value: Zegama-id20025 ville=ZEGAMA


Help is welcome.

jcdole
17th October 2013, 14:30
Hello.


From unix.com
Originally Posted by CarloM
Rather than using (.+) to match any character, try using ([^\\s]+) to match non-whitespace characters (as in the earlier sed suggestions).



QRegularExpression re1("code=(.+)\\s");

becomes :


QRegularExpression re1("code=([^\\s]+)");