O'Reilly logo

Game Programming using Qt 5 Beginner's Guide - Second Edition by Lorenz Haas, Witold Wysota, Pavel Strakhov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Finding all pattern occurrences

Lastly, let's take a final look at how to find, for example, all numbers inside a string, even those leading with zeros:

QString input = QStringLiteral("123 foo 09 1a 3");
QRegularExpression regex("\\b\\d+\\b");
QRegularExpressionMatchIterator i = regex.globalMatch(input);
while (i.hasNext()) {
    QRegularExpressionMatch match = i.next();
    qDebug() << match.captured();
}

The input string contains an exemplary text in which we would like to find all numbers. The "foo" as well as "1a" variables should not be found by the pattern, since these are not valid numbers. Therefore, we set up the pattern, defining that we require at least one digit, \d+, and that this digit—or these digits—should be wrapped by word boundaries, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required