QRegExprxlen("(\\d+)(?:\\s*)(cm|inch)"); int pos3 = rxlen.indexIn("Length:189cm"); if (pos3 > -1) { QString value = rxlen.cap(1); // 结果为189 QString unit = rxlen.cap(2); // 结果为cm QString string = rxlen.cap(0); // 结果为189cm qDebug() << value << unit<< string; } 这个正则表达式我大概能看懂。是一个单词,加可能的“:”,加非字母,加cm或inch。 不过我一直不能理解cap这个函数的意思,这里为什么得到这个结果 如果我加个ok的单词改为: QRegExprxlen("(\\d+)(?:\\s*)(ok)(cm|inch)"); int pos3 = rxlen.indexIn("Length: ok 189cm");. if (pos3 > -1) { QString value = rxlen.cap(1); QString unit = rxlen.cap(2); QString string = rxlen.cap(0); qDebug() << value << unit<< string; } 结果就没有了。。。。
Returns the text captured by the nth subexpression. The entire match has index 0 and the parenthesized subexpressions have indexes starting from 1 (excluding non-capturing parentheses).
rxlen("(\\d+)(?:\\s*)(cm|inch)"); int pos = rxlen.("Length: 189cm"); if (pos > -1) { value = rxlen.cap(1); // "189" unit = rxlen.cap(2); // "cm" // ... }The order of elements matched by cap() is as follows. The first element, cap(0), is the entire matching string. Each subsequent element corresponds to the next capturing open left parentheses. Thus cap(1) is the text of the first capturing parentheses, cap(2) is the text of the second, and so on.
求解
|