内容表

上一话题

QRegularExpression

下一话题

QRegularExpressionMatchIterator

QRegularExpressionMatch

QRegularExpressionMatch class provides the results of a matching a QRegularExpression 与字符串。 更多

Inheritance diagram of PySide2.QtCore.QRegularExpressionMatch

概要

函数

详细描述

A QRegularExpressionMatch object can be obtained by calling the match() function, or as a single result of a global match from a QRegularExpressionMatchIterator .

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

此外, 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() will return all the captured substrings at once (including the substring matched by the entire pattern) in the order they have been captured by captring 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 正则表达式类的更多信息。

class QRegularExpressionMatch

QRegularExpressionMatch(match)

param match

QRegularExpressionMatch

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

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

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

另请参阅

operator=()

PySide2.QtCore.QRegularExpressionMatch. captured ( name )
参数

name – unicode

返回类型

unicode

PySide2.QtCore.QRegularExpressionMatch. captured ( [ nth=0 ] )
参数

nth int

返回类型

unicode

Returns the substring captured by the nth capturing group.

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.

PySide2.QtCore.QRegularExpressionMatch. capturedEnd ( name )
参数

name – unicode

返回类型

int

PySide2.QtCore.QRegularExpressionMatch. capturedEnd ( [ nth=0 ] )
参数

nth int

返回类型

int

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.

PySide2.QtCore.QRegularExpressionMatch. capturedLength ( name )
参数

name – unicode

返回类型

int

PySide2.QtCore.QRegularExpressionMatch. capturedLength ( [ nth=0 ] )
参数

nth int

返回类型

int

Returns the length of the substring captured by the nth capturing group.

注意

This function returns 0 if the nth capturing group did not capture a string or doesn’t exist.

PySide2.QtCore.QRegularExpressionMatch. capturedRef ( name )
参数

name – unicode

返回类型

QStringRef

PySide2.QtCore.QRegularExpressionMatch. capturedRef ( [ nth=0 ] )
参数

nth int

返回类型

QStringRef

Returns a reference to the substring captured by the nth capturing group.

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

注意

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

PySide2.QtCore.QRegularExpressionMatch. capturedStart ( [ nth=0 ] )
参数

nth int

返回类型

int

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.

PySide2.QtCore.QRegularExpressionMatch. capturedStart ( name )
参数

name – unicode

返回类型

int

PySide2.QtCore.QRegularExpressionMatch. capturedTexts ( )
返回类型

字符串列表

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.

PySide2.QtCore.QRegularExpressionMatch. hasMatch ( )
返回类型

bool

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

PySide2.QtCore.QRegularExpressionMatch. hasPartialMatch ( )
返回类型

bool

返回 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.

另请参阅

match() MatchType hasMatch()

PySide2.QtCore.QRegularExpressionMatch. isValid ( )
返回类型

bool

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

PySide2.QtCore.QRegularExpressionMatch. lastCapturedIndex ( )
返回类型

int

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 could have not matched, and therefore captured nothing.

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

PySide2.QtCore.QRegularExpressionMatch. matchOptions ( )
返回类型

MatchOptions

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

PySide2.QtCore.QRegularExpressionMatch. matchType ( )
返回类型

MatchType

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

PySide2.QtCore.QRegularExpressionMatch. regularExpression ( )
返回类型

QRegularExpression

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

PySide2.QtCore.QRegularExpressionMatch. swap ( other )
参数

other QRegularExpressionMatch

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