首页 > 代码库 > QRegExp

QRegExp

 这段代码会越界,百思不得七姐(过了N久时间 之后^^)原来是把i写成了1  --!

//#if 0        QRegExp re1("AT+CGATT?");        QRegExp re2("AT+CGATT=([0-9])");        if (re1.exactMatch(list[1])) {            qDebug()<<"list[i]"<<list[i];        }        else if (re2.exactMatch(list[i])) {            // set            int state;            bool ok;            state = re2.cap(1).toInt(&ok);            if (ok && state >= 0 && state <= 1) {                ue->cgatt.state = state;                response.append("OK");            }            else                response.append("ERROR");        }//#endif

 

 这个for + switch 的用法回味好久之后还是美

           QRegExp ref1("AT+COPS?");            QRegExp ref2("AT+COPS=([0-9])(.*)"); //[<mode>[,<format> [,<oper>[,<AcT>]]]]            if (ref1.exactMatch(list[i]))                response.append("+COPS:")                        .append(QString::number(ue->cops.mode)).append(",")                        .append(QString::number(ue->cops.format)).append(",")                        .append(ue->cops.oper_numeric).append(",")                        .append(QString::number(ue->cops.act)).append("\r\nOK");            else if (ref2.exactMatch(list[i]))            {                   ue->cops.mode = ref2.cap(1).toInt();                if (!ref2.cap(2).isEmpty()) {                    QStringList pa;                    pa = ref2.cap(2).split(",");                    for (i = 0; i < pa.size(); i++) {                        switch (i) {                        case 0: ue->cops.format = pa.at(i).toInt(); break;                        case 1: strcpy(ue->cops.oper_long, pa.at(i).toAscii().constData()); break;                        case 2: ue->cops.act = pa.at(i).toInt(); break;                        default:                            break;                        }                    }                }