diff --git a/src/corelib/doc/snippets/code/src_corelib_text_qregularexpression.cpp b/src/corelib/doc/snippets/code/src_corelib_text_qregularexpression.cpp index c418a1f279c..8bf67a9e2db 100644 --- a/src/corelib/doc/snippets/code/src_corelib_text_qregularexpression.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_text_qregularexpression.cpp @@ -357,6 +357,8 @@ QString wildcard = QRegularExpression::wildcardToRegularExpression("*.jpeg"); { //! [34] +// using a raw string literal, R"(raw_characters)", to be able to use "\w" +// without having to escape the backslash as "\\w" QRegularExpression re(R"(\w+)"); QString subject("the quick fox"); for (const QRegularExpressionMatch &match : re.globalMatch(subject)) { @@ -364,4 +366,12 @@ for (const QRegularExpressionMatch &match : re.globalMatch(subject)) { } //! [34] } + +{ +//! [35] +// matches two digits followed by a space and a word +QRegularExpression re(R"(\d\d \w+)"); +//! [35] +} + } diff --git a/src/corelib/text/qregularexpression.cpp b/src/corelib/text/qregularexpression.cpp index 827884d7094..6cc90118224 100644 --- a/src/corelib/text/qregularexpression.cpp +++ b/src/corelib/text/qregularexpression.cpp @@ -140,6 +140,14 @@ QT_BEGIN_NAMESPACE \snippet code/src_corelib_text_qregularexpression.cpp 2 + Alternatively, you can use a + \l {https://en.cppreference.com/w/cpp/language/string_literal} {raw string literal}, + in which case you don't need to escape backslashes in the pattern, all characters + between \c {R"(...)"} are considered raw characters. As you can see in the following + example, this simplifies writing patterns: + + \snippet code/src_corelib_text_qregularexpression.cpp 35 + The pattern() function returns the pattern that is currently set for a QRegularExpression object: