diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2021-08-06 10:17:50 +0200 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2021-08-25 00:06:42 +0200 |
commit | bac329a28b14ccddcdb45a4ef51cd8056eda9233 (patch) | |
tree | 06aa44b30eb36fd441be11215f73a98885d120d2 /src/corelib/doc/snippets/code | |
parent | 145940e1ef8fc8334ff4603a44f7896886e646cb (diff) |
QRegularExpressionMatch: add a way to know if a capturing group captured
Relying on the fact that a given capturing group captured a null string
doesn't allow users to distinguish whether a capturing group did not
capture anything, or captured a null substring (say, from a null subject
string).
Perl allows for the distinction: the entries in the @- and @+ arrays are
set to values in case there is a capture, but they're undef otherwise.
PCRE2 gives us the information already in the results "ovector", but
it was simply not exposed to QREM users. So, expose it.
[ChangeLog][QtCore][QRegularExpressionMatch] Added the hasCaptured()
family of functions to know if a given capturing group has captured
something.
Change-Id: Ic1320933d4554e2e313c0a680be1b1b9dd95af0b
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/corelib/doc/snippets/code')
-rw-r--r-- | src/corelib/doc/snippets/code/src_corelib_text_qregularexpression.cpp | 12 |
1 files changed, 12 insertions, 0 deletions
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 8bf67a9e2d..3bb9727ca6 100644 --- a/src/corelib/doc/snippets/code/src_corelib_text_qregularexpression.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_text_qregularexpression.cpp @@ -374,4 +374,16 @@ QRegularExpression re(R"(\d\d \w+)"); //! [35] } +{ +//! [36] +QRegularExpression re("([a-z]+)|([A-Z]+)"); +QRegularExpressionMatch m = re.match("UPPERCASE"); +if (m.hasMatch()) { + qDebug() << m.hasCaptured(0); // true + qDebug() << m.hasCaptured(1); // false + qDebug() << m.hasCaptured(2); // true +} +//! [36] +} + } |