QRegularExpression: mention raw string literal in the docs
Raw string literals (since C++11 according to [1]), make writing/reading regex patterns easier, since one can just use e.g. "\w\d" without having to escape those backslashes e.g. "\\w\\d"; this is especially useful with longer/more complex regex patterns. [1] https://en.cppreference.com/w/cpp/language/string_literal Change-Id: I119f9566d27222b915af931ee7e13e064baede61 Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> Reviewed-by: André Hartmann <aha_1980@gmx.de> (cherry picked from commit 824c726340ed8da0393aa97016682bcfd8c8bdc3) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
dae30b73c5
commit
1bfefd56a1
@ -357,6 +357,8 @@ QString wildcard = QRegularExpression::wildcardToRegularExpression("*.jpeg");
|
|||||||
|
|
||||||
{
|
{
|
||||||
//! [34]
|
//! [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+)");
|
QRegularExpression re(R"(\w+)");
|
||||||
QString subject("the quick fox");
|
QString subject("the quick fox");
|
||||||
for (const QRegularExpressionMatch &match : re.globalMatch(subject)) {
|
for (const QRegularExpressionMatch &match : re.globalMatch(subject)) {
|
||||||
@ -364,4 +366,12 @@ for (const QRegularExpressionMatch &match : re.globalMatch(subject)) {
|
|||||||
}
|
}
|
||||||
//! [34]
|
//! [34]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
//! [35]
|
||||||
|
// matches two digits followed by a space and a word
|
||||||
|
QRegularExpression re(R"(\d\d \w+)");
|
||||||
|
//! [35]
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -140,6 +140,14 @@ QT_BEGIN_NAMESPACE
|
|||||||
|
|
||||||
\snippet code/src_corelib_text_qregularexpression.cpp 2
|
\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
|
The pattern() function returns the pattern that is currently set for a
|
||||||
QRegularExpression object:
|
QRegularExpression object:
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user