QRegularExpressionMatch 类

QRegularExpressionMatch 类提供的结果,匹配 QRegularExpression 与字符串。 更多...

头: #include <QRegularExpressionMatch>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

注意: 此类的所有函数 可重入 .

公共函数

QRegularExpressionMatch ()
QRegularExpressionMatch (const QRegularExpressionMatch & match )
QRegularExpressionMatch (QRegularExpressionMatch && match )
~QRegularExpressionMatch ()
QString captured (int nth = 0) const
QString captured (const QString & name ) const
QString captured (QStringView name ) const
qsizetype capturedEnd (int nth = 0) const
qsizetype capturedEnd (const QString & name ) const
qsizetype capturedEnd (QStringView name ) const
qsizetype capturedLength (int nth = 0) const
qsizetype capturedLength (const QString & name ) const
qsizetype capturedLength (QStringView name ) const
qsizetype capturedStart (int nth = 0) const
qsizetype capturedStart (const QString & name ) const
qsizetype capturedStart (QStringView name ) const
QStringList capturedTexts () const
QStringView capturedView (int nth = 0) const
QStringView capturedView (QStringView name ) const
bool hasCaptured (const QString & name ) const
bool hasCaptured (QStringView name ) const
bool hasCaptured (int nth ) const
bool hasMatch () const
bool hasPartialMatch () const
bool isValid () const
int lastCapturedIndex () const
QRegularExpression::MatchOptions matchOptions () const
QRegularExpression::MatchType matchType () const
QRegularExpression regularExpression () const
void swap (QRegularExpressionMatch & other )
QRegularExpressionMatch & operator= (const QRegularExpressionMatch & match )
QRegularExpressionMatch & operator= (QRegularExpressionMatch && match )
QDebug operator<< (QDebug debug , const QRegularExpressionMatch & match )

详细描述

QRegularExpressionMatch 对象可以被获得,通过调用 QRegularExpression::match () 函数,或作为全局匹配的单个结果来自 QRegularExpressionMatchIterator .

The success or the failure of a match attempt can be inspected by calling the hasMatch () function. QRegularExpressionMatch also reports a successful partial match through the hasPartialMatch () 函数。

In addition, QRegularExpressionMatch returns the substrings captured by the capturing groups in the pattern string. The implicit capturing group with index 0 captures the result of the whole match. The captured () function returns each substring captured, either by the capturing group's index or by its name:

QRegularExpression re("(\\d\\d) (?<name>\\w+)");
QRegularExpressionMatch match = re.match("23 Jordan");
if (match.hasMatch()) {
    QString number = match.captured(1); // first == "23"
    QString name = match.captured("name"); // name == "Jordan"
}
					

For each captured substring it is possible to query its starting and ending offsets in the subject string by calling the capturedStart () 和 capturedEnd () function, respectively. The length of each captured substring is available using the capturedLength () 函数。

The convenience function capturedTexts () 会返回 all the captured substrings at once (including the substring matched by the entire pattern) in the order they have been captured by capturing groups; that is, captured(i) == capturedTexts().at(i) .

可以检索 QRegularExpression object the subject string was matched against by calling the regularExpression () function; the match type and the match options are available as well by calling the matchType () 和 matchOptions () 分别。

请参考 QRegularExpression 文档编制,了解有关 Qt 正则表达式类的更多信息。

另请参阅 QRegularExpression .

成员函数文档编制

[since 6.3] bool QRegularExpressionMatch:: hasCaptured ( QStringView name ) const

[since 6.3] bool QRegularExpressionMatch:: hasCaptured (const QString & name ) const

Returns true if the capturing group named name captured something in the subject string, and false otherwise (or if there is no capturing group called name ).

注意: Some capturing groups in a regular expression may not have captured anything even if the regular expression matched. This may happen, for instance, if a conditional operator is used in the pattern:

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
}
					

Similarly, a capturing group may capture a substring of length 0; this function will return true for such a capturing group.

该函数在 Qt 6.3 引入。

另请参阅 captured () 和 hasMatch ().

QRegularExpressionMatch:: QRegularExpressionMatch ()

Constructs a valid, empty QRegularExpressionMatch object. The regular expression is set to a default-constructed one; the match type to QRegularExpression::NoMatch and the match options to QRegularExpression::NoMatchOption .

The object will report no match through the hasMatch () 和 hasPartialMatch () member functions.

QRegularExpressionMatch:: QRegularExpressionMatch (const QRegularExpressionMatch & match )

Constructs a match result by copying the result of the given match .

另请参阅 operator= ().

[since 6.1] QRegularExpressionMatch:: QRegularExpressionMatch ( QRegularExpressionMatch && match )

Constructs a match result by moving the result from the given match .

Note that a moved-from QRegularExpressionMatch can only be destroyed or assigned to. The effect of calling other functions than the destructor or one of the assignment operators is undefined.

该函数在 Qt 6.1 引入。

另请参阅 operator= ().

QRegularExpressionMatch:: ~QRegularExpressionMatch ()

销毁匹配结果。

QString QRegularExpressionMatch:: captured ( int nth = 0) const

返回子字符串捕获通过 nth 捕获组。

nth capturing group did not capture a string, or if there is no such capturing group, returns a null QString .

注意: The implicit capturing group number 0 captures the substring matched by the entire pattern.

另请参阅 capturedView (), lastCapturedIndex (), capturedStart (), capturedEnd (), capturedLength (),和 QString::isNull ().

QString QRegularExpressionMatch:: captured (const QString & name ) const

Returns the substring captured by the capturing group named name .

If the named capturing group name did not capture a string, or if there is no capturing group named name , returns a null QString .

另请参阅 capturedView (), capturedStart (), capturedEnd (), capturedLength (),和 QString::isNull ().

QString QRegularExpressionMatch:: captured ( QStringView name ) const

Returns the substring captured by the capturing group named name .

If the named capturing group name did not capture a string, or if there is no capturing group named name , returns a null QString .

另请参阅 capturedView (), capturedStart (), capturedEnd (), capturedLength (),和 QString::isNull ().

qsizetype QRegularExpressionMatch:: capturedEnd ( int nth = 0) const

Returns the offset inside the subject string immediately after the ending position of the substring captured by the nth capturing group. If the nth capturing group did not capture a string or doesn't exist, returns -1.

另请参阅 capturedStart (), capturedLength (),和 captured ().

qsizetype QRegularExpressionMatch:: capturedEnd (const QString & name ) const

Returns the offset inside the subject string immediately after the ending position of the substring captured by the capturing group named name . If the capturing group named name did not capture a string or doesn't exist, returns -1.

另请参阅 capturedStart (), capturedLength (),和 captured ().

qsizetype QRegularExpressionMatch:: capturedEnd ( QStringView name ) const

Returns the offset inside the subject string immediately after the ending position of the substring captured by the capturing group named name . If the capturing group named name did not capture a string or doesn't exist, returns -1.

另请参阅 capturedStart (), capturedLength (),和 captured ().

qsizetype QRegularExpressionMatch:: capturedLength ( int nth = 0) const

Returns the length of the substring captured by the nth 捕获组。

注意: This function returns 0 if the nth capturing group did not capture a string or doesn't exist.

另请参阅 capturedStart (), capturedEnd (),和 captured ().

qsizetype QRegularExpressionMatch:: capturedLength (const QString & name ) const

Returns the length of the substring captured by the capturing group named name .

注意: This function returns 0 if the capturing group named name did not capture a string or doesn't exist.

另请参阅 capturedStart (), capturedEnd (),和 captured ().

qsizetype QRegularExpressionMatch:: capturedLength ( QStringView name ) const

Returns the length of the substring captured by the capturing group named name .

注意: This function returns 0 if the capturing group named name did not capture a string or doesn't exist.

另请参阅 capturedStart (), capturedEnd (),和 captured ().

qsizetype QRegularExpressionMatch:: capturedStart ( int nth = 0) const

Returns the offset inside the subject string corresponding to the starting position of the substring captured by the nth capturing group. If the nth capturing group did not capture a string or doesn't exist, returns -1.

另请参阅 capturedEnd (), capturedLength (),和 captured ().

qsizetype QRegularExpressionMatch:: capturedStart (const QString & name ) const

Returns the offset inside the subject string corresponding to the starting position of the substring captured by the capturing group named name . If the capturing group named name did not capture a string or doesn't exist, returns -1.

另请参阅 capturedEnd (), capturedLength (),和 captured ().

qsizetype QRegularExpressionMatch:: capturedStart ( QStringView name ) const

Returns the offset inside the subject string corresponding to the starting position of the substring captured by the capturing group named name . If the capturing group named name did not capture a string or doesn't exist, returns -1.

另请参阅 capturedEnd (), capturedLength (),和 captured ().

QStringList QRegularExpressionMatch:: capturedTexts () const

Returns a list of all strings captured by capturing groups, in the order the groups themselves appear in the pattern string. The list includes the implicit capturing group number 0, capturing the substring matched by the entire pattern.

QStringView QRegularExpressionMatch:: capturedView ( int nth = 0) const

Returns a view of the substring captured by the nth 捕获组。

nth capturing group did not capture a string, or if there is no such capturing group, returns a null QStringView .

注意: The implicit capturing group number 0 captures the substring matched by the entire pattern.

另请参阅 captured (), lastCapturedIndex (), capturedStart (), capturedEnd (), capturedLength (),和 QStringView::isNull ().

QStringView QRegularExpressionMatch:: capturedView ( QStringView name ) const

Returns a view of the string captured by the capturing group named name .

If the named capturing group name did not capture a string, or if there is no capturing group named name , returns a null QStringView .

另请参阅 captured (), capturedStart (), capturedEnd (), capturedLength (),和 QStringView::isNull ().

[since 6.3] bool QRegularExpressionMatch:: hasCaptured ( int nth ) const

返回 true 若 nth capturing group captured something in the subject string, and false otherwise (or if there is no such capturing group).

注意: The implicit capturing group number 0 captures the substring matched by the entire pattern.

注意: Some capturing groups in a regular expression may not have captured anything even if the regular expression matched. This may happen, for instance, if a conditional operator is used in the pattern:

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
}
					

Similarly, a capturing group may capture a substring of length 0; this function will return true for such a capturing group.

该函数在 Qt 6.3 引入。

另请参阅 captured (), lastCapturedIndex (),和 hasMatch ().

bool QRegularExpressionMatch:: hasMatch () const

返回 true if the regular expression matched against the subject string, or false otherwise.

另请参阅 QRegularExpression::match () 和 hasPartialMatch ().

bool QRegularExpressionMatch:: hasPartialMatch () const

返回 true if the regular expression partially matched against the subject string, or false otherwise.

注意: Only a match that explicitly used the one of the partial match types can yield a partial match. Still, if such a match succeeds totally, this function will return false, while hasMatch () will return true.

另请参阅 QRegularExpression::match (), QRegularExpression::MatchType ,和 hasMatch ().

bool QRegularExpressionMatch:: isValid () const

返回 true if the match object was obtained as a result from the QRegularExpression::match () function invoked on a valid QRegularExpression 对象;返回 false QRegularExpression was invalid.

另请参阅 QRegularExpression::match () 和 QRegularExpression::isValid ().

int QRegularExpressionMatch:: lastCapturedIndex () const

Returns the index of the last capturing group that captured something, including the implicit capturing group 0. This can be used to extract all the substrings that were captured:

QRegularExpressionMatch match = re.match(string);
for (int i = 0; i <= match.lastCapturedIndex(); ++i) {
    QString captured = match.captured(i);
    // ...
}
					

Note that some of the capturing groups with an index less than lastCapturedIndex() could have not matched, and therefore captured nothing.

If the regular expression did not match, this function returns -1.

另请参阅 hasCaptured (), captured (), capturedStart (), capturedEnd (),和 capturedLength ().

QRegularExpression::MatchOptions QRegularExpressionMatch:: matchOptions () const

Returns the match options that were used to get this QRegularExpressionMatch object, that is, the match options that were passed to QRegularExpression::match () 或 QRegularExpression::globalMatch ().

另请参阅 QRegularExpression::match (), regularExpression (),和 matchType ().

QRegularExpression::MatchType QRegularExpressionMatch:: matchType () const

Returns the match type that was used to get this QRegularExpressionMatch object, that is, the match type that was passed to QRegularExpression::match () 或 QRegularExpression::globalMatch ().

另请参阅 QRegularExpression::match (), regularExpression (),和 matchOptions ().

QRegularExpression QRegularExpressionMatch:: regularExpression () const

返回 QRegularExpression object whose match() function returned this object.

另请参阅 QRegularExpression::match (), matchType (),和 matchOptions ().

void QRegularExpressionMatch:: swap ( QRegularExpressionMatch & other )

Swaps the match result other with this match result. This operation is very fast and never fails.

QRegularExpressionMatch &QRegularExpressionMatch:: operator= (const QRegularExpressionMatch & match )

Assigns the match result match to this object, and returns a reference to the copy.

QRegularExpressionMatch &QRegularExpressionMatch:: operator= ( QRegularExpressionMatch && match )

Move-assigns the match result match to this object, and returns a reference to the result.

Note that a moved-from QRegularExpressionMatch can only be destroyed or assigned to. The effect of calling other functions than the destructor or one of the assignment operators is undefined.

相关非成员

QDebug operator<< ( QDebug debug , const QRegularExpressionMatch & match )

Writes the match object match 到 debug 对象 debug 为调试目的。

另请参阅 调试技术 .