PySide6.QtCore.QRegularExpressionMatch¶
- class QRegularExpressionMatch¶
- The - QRegularExpressionMatchclass provides the results of a matching a- QRegularExpressionagainst a string. More…- Synopsis¶- Methods¶- def - __init__()
- def - captured()
- def - capturedEnd()
- def - capturedLength()
- def - capturedStart()
- def - capturedTexts()
- def - capturedView()
- def - hasCaptured()
- def - hasMatch()
- def - isValid()
- def - matchOptions()
- def - matchType()
- def - swap()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - Detailed Description¶- Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - A - QRegularExpressionMatchobject 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()function.- QRegularExpressionMatchalso reports a successful partial match through the- hasPartialMatch()function.- In addition, - QRegularExpressionMatchreturns 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:- re = QRegularExpression("(\\d\\d) (?\\w+)") match = re.match("23 Jordan") if match.hasMatch(): number = match.captured(1) # first == "23" 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()and the- capturedEnd()function, respectively. The length of each captured substring is available using the- capturedLength()function.- 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 capturing groups; that is,- captured(i) == capturedTexts().at(i).- You can retrieve the - QRegularExpressionobject 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()and the- matchOptions()respectively.- Please refer to the - QRegularExpressiondocumentation for more information about the Qt regular expression classes.- See also - __init__()¶
 - Constructs a valid, empty - QRegularExpressionMatchobject. The regular expression is set to a default-constructed one; the match type to- NoMatchand the match options to- NoMatchOption.- The object will report no match through the - hasMatch()and the- hasPartialMatch()member functions.- __init__(match)
- Parameters:
- match – - QRegularExpressionMatch
 
 - Constructs a match result by copying the result of the given - match.- See also - operator=()- captured(name)¶
- Parameters:
- name – str 
- Return type:
- str 
 
 - Returns the substring captured by the capturing group named - name.- If the named capturing group - namedid not capture a string, or if there is no capturing group named- name, returns a null- QString.- Note - In Qt versions prior to 6.8, this function took - QStringor- QStringView, not- QAnyStringView.- See also - capturedView()- capturedStart()- capturedEnd()- capturedLength()- isNull()- captured([nth=0])
- Parameters:
- nth – int 
- Return type:
- str 
 
 - Returns the substring captured by the - nthcapturing group.- If the - nthcapturing group did not capture a string, or if there is no such capturing group, returns a null- QString.- Note - The implicit capturing group number 0 captures the substring matched by the entire pattern. - See also - capturedView()- lastCapturedIndex()- capturedStart()- capturedEnd()- capturedLength()- isNull()- capturedEnd(name)¶
- Parameters:
- name – str 
- Return type:
- int 
 
 - 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- namedid not capture a string or doesn’t exist, returns -1.- Note - In Qt versions prior to 6.8, this function took - QStringor- QStringView, not- QAnyStringView.- See also - capturedEnd([nth=0])
- Parameters:
- nth – int 
- Return type:
- int 
 
 - Returns the offset inside the subject string immediately after the ending position of the substring captured by the - nthcapturing group. If the- nthcapturing group did not capture a string or doesn’t exist, returns -1.- See also - capturedLength(name)¶
- Parameters:
- name – str 
- Return type:
- int 
 
 - Returns the length of the substring captured by the capturing group named - name.- Note - This function returns 0 if the capturing group named - namedid not capture a string or doesn’t exist.- Note - In Qt versions prior to 6.8, this function took - QStringor- QStringView, not- QAnyStringView.- See also - capturedLength([nth=0])
- Parameters:
- nth – int 
- Return type:
- int 
 
 - Returns the length of the substring captured by the - nthcapturing group.- Note - This function returns 0 if the - nthcapturing group did not capture a string or doesn’t exist.- See also - capturedStart(name)¶
- Parameters:
- name – str 
- Return type:
- int 
 
 - 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- namedid not capture a string or doesn’t exist, returns -1.- Note - In Qt versions prior to 6.8, this function took - QStringor- QStringView, not- QAnyStringView.- See also - capturedStart([nth=0])
- Parameters:
- nth – int 
- Return type:
- int 
 
 - Returns the offset inside the subject string corresponding to the starting position of the substring captured by the - nthcapturing group. If the- nthcapturing group did not capture a string or doesn’t exist, returns -1.- See also - capturedTexts()¶
- Return type:
- list of strings 
 
 - 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. - capturedView(name)¶
- Parameters:
- name – str 
- Return type:
- str 
 
 - Returns a view of the string captured by the capturing group named - name.- If the named capturing group - namedid not capture a string, or if there is no capturing group named- name, returns a null- QStringView.- Note - In Qt versions prior to 6.8, this function took - QStringor- QStringView, not- QAnyStringView.- See also - captured()- capturedStart()- capturedEnd()- capturedLength()- isNull()- capturedView([nth=0])
- Parameters:
- nth – int 
- Return type:
- str 
 
 - Returns a view of the substring captured by the - nthcapturing group.- If the - nthcapturing group did not capture a string, or if there is no such capturing group, returns a null- QStringView.- Note - The implicit capturing group number 0 captures the substring matched by the entire pattern. - See also - captured()- lastCapturedIndex()- capturedStart()- capturedEnd()- capturedLength()- isNull()- hasCaptured(name)¶
- Parameters:
- name – str 
- Return type:
- bool 
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Returns true if the capturing group named - namecaptured something in the subject string, and false otherwise (or if there is no capturing group called- name).- Note - 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: - re = QRegularExpression("([a-z]+)|([A-Z]+)") m = re.match("UPPERCASE") if m.hasMatch(): print(m.hasCaptured(0)) # true print(m.hasCaptured(1)) # false print(m.hasCaptured(2)) # true - Similarly, a capturing group may capture a substring of length 0; this function will return - truefor such a capturing group.- Note - In Qt versions prior to 6.8, this function took - QStringor- QStringView, not- QAnyStringView.- See also - hasCaptured(nth)
- Parameters:
- nth – int 
- Return type:
- bool 
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Returns true if the - nthcapturing group captured something in the subject string, and false otherwise (or if there is no such capturing group).- Note - The implicit capturing group number 0 captures the substring matched by the entire pattern. - Note - 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: - re = QRegularExpression("([a-z]+)|([A-Z]+)") m = re.match("UPPERCASE") if m.hasMatch(): print(m.hasCaptured(0)) # true print(m.hasCaptured(1)) # false print(m.hasCaptured(2)) # true - Similarly, a capturing group may capture a substring of length 0; this function will return - truefor such a capturing group.- See also - hasMatch()¶
- Return type:
- bool 
 
 - Returns - trueif the regular expression matched against the subject string, or false otherwise.- See also - hasPartialMatch()¶
- Return type:
- bool 
 
 - Returns - trueif the regular expression partially matched against the subject string, or false otherwise.- Note - 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.- See also - isValid()¶
- Return type:
- bool 
 
 - Returns - trueif the match object was obtained as a result from the- match()function invoked on a valid- QRegularExpressionobject; returns- falseif the- QRegularExpressionwas invalid.- lastCapturedIndex()¶
- Return type:
- int 
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - 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: - match = re.match(string) for i in range(0, match.lastCapturedIndex() + 1): 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. - matchOptions()¶
- Return type:
- Combination of - MatchOption
 
 - Returns the match options that were used to get this - QRegularExpressionMatchobject, that is, the match options that were passed to- match()or- globalMatch().- See also - Returns the match type that was used to get this - QRegularExpressionMatchobject, that is, the match type that was passed to- match()or- globalMatch().- See also - regularExpression()¶
- Return type:
 
 - Returns the - QRegularExpressionobject whose match() function returned this object.- See also - swap(other)¶
- Parameters:
- other – - QRegularExpressionMatch
 
 - Swaps this match result with - other. This operation is very fast and never fails.