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, ...

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.