#include#include int main() { QRegExp rx("hello"); QString str = "hello world"; if (rx.indexIn(str) != -1) { qDebug() << "Matched length: " << rx.matchedLength(); } // Output: Matched length: 5 }
#includeIn this example, we create a QRegExp object that matches a pattern containing a number followed by any number of whitespaces, followed by a word. We then search for this pattern within the string "123 abc 456 def". The indexIn method is used within a while loop to find all the occurrences of the pattern within the string. The cap method is used to retrieve different groups of the matched pattern, in this case, we retrieve the word following the number. We output both the matched word and matched string along with their lengths using the matchedLength method. Package/library: Qt (C++ framework)#include int main() { QRegExp rx("\\d+\\s*(\\w+)"); QString str = "123 abc 456 def"; int pos = 0; while ((pos = rx.indexIn(str, pos)) != -1) { qDebug() << "Matched word: " << rx.cap(1) << ", length: " << rx.cap(1).length(); qDebug() << "Matched string: " << rx.cap() << ", length: " << rx.cap().length(); pos += rx.matchedLength(); } /* Output: Matched word: "abc", length: 3 Matched string: "123 abc", length: 11 Matched word: "def", length: 3 Matched string: "456 def", length: 9 */ }