From 15a1d36b1a209e7f169955d7f8f1b64115d20e6e Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Sun, 23 Jun 2019 15:20:33 +0200 Subject: [PATCH] QRegularExpression docs: streamline the anchoredPattern section Users may get confounded by the "essay" about exact matching. We now have a function to build a pattern to do exact matching, just state that that's the solution, end of the story. Change-Id: I0a72aa2255af50a1991540b834f146b6e6bc912e Reviewed-by: Thiago Macieira Reviewed-by: Samuel Gaist --- .../src_corelib_tools_qregularexpression.cpp | 12 +++----- src/corelib/tools/qregularexpression.cpp | 29 +++---------------- 2 files changed, 8 insertions(+), 33 deletions(-) diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qregularexpression.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qregularexpression.cpp index afdd9c3d25..99cd4ea7a2 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qregularexpression.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qregularexpression.cpp @@ -286,15 +286,11 @@ if (!invalidRe.isValid()) { { //! [24] -QRegularExpression re("^this pattern must match exactly$"); -//! [24] -} - -{ -//! [25] QString p("a .*|pattern"); -QRegularExpression re("\\A(?:" + p + ")\\z"); // re matches exactly the pattern string p -//! [25] + +// re matches exactly the pattern string p +QRegularExpression re(QRegularExpression::anchoredPattern(p)); +//! [24] } { diff --git a/src/corelib/tools/qregularexpression.cpp b/src/corelib/tools/qregularexpression.cpp index 9c201e770b..17acd476b2 100644 --- a/src/corelib/tools/qregularexpression.cpp +++ b/src/corelib/tools/qregularexpression.cpp @@ -460,34 +460,13 @@ QT_BEGIN_NAMESPACE \row \li \c{"[a-z]+\\d+"} \li \b true \li \b true \endtable - Exact matching is not reflected in QRegularExpression. If you want to be - sure that the subject string matches the regular expression exactly, you can wrap the - pattern between a couple of anchoring expressions. Simply - putting the pattern between the \c{^} and the \c{$} anchors is enough - in most cases: + Exact matching is not reflected in QRegularExpression. If you want + to be sure that the subject string matches the regular expression + exactly, you can wrap the pattern using the anchoredPattern() + function: \snippet code/src_corelib_tools_qregularexpression.cpp 24 - However, remember that the \c{$} anchor not only matches at the end of the - string, but also at a newline character right before the end of the string; - that is, the previous pattern matches against the string "this pattern must - match exactly\\n". Also, the behaviour of both the \c{^} and the \c{$} - anchors changes if the MultiLineOption is set either explicitly (as a - pattern option) or implicitly (as a directive inside the pattern string). - - Therefore, in the most general case, you should wrap the pattern between - the \c{\A} and the \c{\z} anchors: - - \snippet code/src_corelib_tools_qregularexpression.cpp 25 - - Note the usage of the non-capturing group in order to preserve the meaning - of the branch operator inside the pattern. - - The QRegularExpression::anchoredPattern() helper method does exactly that for - you. - - \sa anchoredPattern - \section3 Porting from QRegExp's Partial Matching When using QRegExp::exactMatch(), if an exact match was not found, one