Important: Please read the Qt Code of Conduct -

[SOLVED] Problem with QRegularExpression

  • Hello. I have a problem with regex. The following function crashes my program:

    QString Metar::getTemperature()

    QString temp;
    QString metarReport = "CYOW 171900Z 33329G45MPS 15SM SCT050 24/M13 A3013 RMK CU3 SLP251 DENSITY ALT 1200FT";
    // If Metar report has not been downloaded properly
    if (metarReport == "N/A")
        temperature = "N/A";
        QRegularExpression rx("(^|\\s)M?\\d+/M?\\d+($|\\s)");
        QRegularExpressionMatch match = rx.match(metarReport);
        if (match.hasMatch())
            temp = match.captured(1);
        // The resulting string has space in the beginning. We remove the space.
        temp.replace(" ","");
        // If temperature is below zero we need to replace "M" with "-"
        temp.replace('M', '-' );
        // We want only the first part of the string. The second part is dew.
        QStringList tokens = temp.split("/");
        temperature = tokens[1];
    return temperature;


    It should return 24.
    I tried it with QRegEx -- same problem.

    Thanks for your help!

  • Hi,
    Are you sure that the length of your tokens is greater than or equal to 2 ?

  • Hi,

    I changed to

    temp = match.captured(0);

    I do not have this problem now. No crash.



Log in to reply