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 <thiago.macieira@intel.com>
Reviewed-by: Samuel Gaist <samuel.gaist@idiap.ch>
This commit is contained in:
Giuseppe D'Angelo 2019-06-23 15:20:33 +02:00
parent 7eb070b450
commit 15a1d36b1a
2 changed files with 8 additions and 33 deletions

View File

@ -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]
}
{

View File

@ -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