[SOLVED] Problem with QRegularExpression



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

    [code]
    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";
    }
    
    else
    {
        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;
    

    }
    [/code]

    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.

    Thanks!

    Igor


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.