CopperSpice API  1.9.1
QRegularExpressionMatch< S > Class Template Reference

Provides the results of matching a QRegularExpression for a given string. More...

Public Methods

 QRegularExpressionMatch () = default
 
 QRegularExpressionMatch (const QRegularExpressionMatch &other) = default
 
 QRegularExpressionMatch (QRegularExpressionMatch &&other) = default
 
captured (const S &name) const
 
captured (int index=0) const
 
captured (QStringView< S > name) const
 
S::const_iterator capturedEnd (const S &name) const
 
S::const_iterator capturedEnd (int index=0) const
 
S::const_iterator capturedEnd (QStringView< S > name) const
 
int capturedLength (const S &name) const
 
int capturedLength (int index=0) const
 
int capturedLength (QStringView< S > name) const
 
S::const_iterator capturedStart (const S &name) const
 
S::const_iterator capturedStart (int index=0) const
 
S::const_iterator capturedStart (QStringView< S > name) const
 
QList< S > capturedTexts () const
 
QStringView< S > capturedView (const S &name) const
 
QStringView< S > capturedView (int index=0) const
 
QStringView< S > capturedView (QStringView< S > name) const
 
int captureIndexForName (const S &name) const
 
int captureIndexForName (QStringView< S > name) const
 
bool hasMatch () const
 
bool hasPartialMatch () const
 
bool isValid () const
 
int lastCapturedIndex () const
 
QMatchOptionFlags matchOptions () const
 
QMatchType matchType () const
 
QRegularExpressionMatch & operator= (const QRegularExpressionMatch &other) = default
 
QRegularExpressionMatch & operator= (QRegularExpressionMatch &&other) = default
 
void swap (QRegularExpressionMatch &other)
 

Detailed Description

template<typename S>
class QRegularExpressionMatch< S >

A QRegularExpressionMatch can be obtained by calling QRegularExpression::match() or as a single result of a QRegularExpression::globalMatch(). The success or failure of a match attempt can be inspected by calling hasMatch() or 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() method returns each substring captured, either by the capturing group's index or by its name.

QRegularExpression regExp("(\\d\\d) (?<name>\\w+)");
QRegularExpressionMatch match = regExp.match("35 Bob");
if (match.hasMatch()) {
QString number = match.captured(1); // number = "35"
QString name = match.captured("name"); // name = "Bob"
}

For each captured substring you can query the starting and ending offsets in the pattern by calling capturedStart() and capturedEnd(), respectively. The length of each captured substring is available using the capturedLength() method.

Retrieve Captures as a QList

The convenience method capturedTexts() will return all the captured substrings at once (including the substring matched by the entire pattern) in order. The result is a QList.

// equivalent functionality
match.captured(i) == match.capturedTexts().at(i);
See also
QRegularExpression

Constructor & Destructor Documentation

template<typename S >
QRegularExpressionMatch< S >::QRegularExpressionMatch ( )
default

Constructs a valid empty QRegularExpressionMatch object. An empty QRegularExpressionMatch has no captures and hasMatch() will return false.

template<typename S >
QRegularExpressionMatch< S >::QRegularExpressionMatch ( const QRegularExpressionMatch< S > &  other)
default

Copy constructs a new QRegularExpressionMatch from other.

See also
operator=()
template<typename S >
QRegularExpressionMatch< S >::QRegularExpressionMatch ( QRegularExpressionMatch< S > &&  other)
default

Move constructs a new of QRegularExpressionMatch from other.

See also
operator=()

Method Documentation

template<typename S >
S QRegularExpressionMatch< S >::captured ( const S &  name) const

Returns the substring captured by the capturing group name. If the named capturing group did not capture a string or if there is no capturing group matching the specified name, an empty string is returned.

See also
capturedView(), capturedStart(), capturedEnd(), capturedLength()
template<typename S >
S QRegularExpressionMatch< S >::captured ( int  index = 0) const
inline

Returns the substring captured by the index capturing group.

An index value of 0 will return the entire match. If you want to return the contents of the first capture group pass a value of 1. Pass a value of 2 to return the contents of the second capture group, and so on.

If the index capturing group did not capture a string or if there is no such capturing group then an empty string is returned.

See also
capturedView(), lastCapturedIndex(), capturedStart(), capturedEnd(), capturedLength()
template<typename S >
S QRegularExpressionMatch< S >::captured ( QStringView< S >  name) const

Returns the substring captured by the capturing string view group name. If the named capturing group did not capture a string or if there is no capturing group matching the specified name, returns an empty string.

See also
capturedView(), capturedStart(), capturedEnd(), capturedLength()
template<typename S >
S::const_iterator QRegularExpressionMatch< S >::capturedEnd ( const S &  name) const

Returns the position in the string immediately after the last code point captured by the capturing group name. If the capturing group did not capture a string or does not exist, returns end().

See also
capturedStart(), capturedLength(), captured()
template<typename S >
S::const_iterator QRegularExpressionMatch< S >::capturedEnd ( int  index = 0) const

Returns the position in the string immediately after the last code point captured by the index capturing group. If the capturing group did not capture a string or does not exist, returns end().

See also
capturedStart(), capturedLength(), captured()
template<typename S >
S::const_iterator QRegularExpressionMatch< S >::capturedEnd ( QStringView< S >  name) const

Returns the position in the string immediately after the last code point captured by the capturing group name. If the capturing group did not capture a string or does not exist, returns end().

See also
capturedStart(), capturedLength(), captured()
template<typename S >
int QRegularExpressionMatch< S >::capturedLength ( const S &  name) const

Returns the length of the substring captured by the capturing group name. Returns 0 if the named capturing group did not capture a string or if there is no capturing group matching the specified name,

See also
capturedStart(), capturedEnd(), captured()
template<typename S >
int QRegularExpressionMatch< S >::capturedLength ( int  index = 0) const

Returns the length of the substring captured by the index capturing group. Returns 0 if the index capturing group did not capture a string or does not exist.

See also
capturedStart(), capturedEnd(), captured()
template<typename S >
int QRegularExpressionMatch< S >::capturedLength ( QStringView< S >  name) const

Returns the length of the substring captured by the capturing group string view name. Returns 0 if the named capturing group did not capture a string or if there is no capturing group matching the specified name,

See also
capturedStart(), capturedEnd(), captured()
template<typename S >
S::const_iterator QRegularExpressionMatch< S >::capturedStart ( const S &  name) const

Returns the position in the string corresponding to the first code point captured by the capturing group name. If the capturing group did not capture a string or does not exist, returns end().

See also
capturedEnd(), capturedLength(), captured()
template<typename S >
S::const_iterator QRegularExpressionMatch< S >::capturedStart ( int  index = 0) const

Returns the position in the string corresponding to the first code point captured by the index capturing group. If the capturing group did not capture a string or does not exist, returns end().

See also
capturedEnd(), capturedLength(), captured()
template<typename S >
S::const_iterator QRegularExpressionMatch< S >::capturedStart ( QStringView< S >  name) const

Returns the position in the string corresponding to the first code point captured by the capturing group name. If the capturing group did not capture a string or does not exist, returns end().

See also
capturedEnd(), capturedLength(), captured()
template<typename S >
QList< S > QRegularExpressionMatch< S >::capturedTexts ( ) const

Returns a list of all strings captured by capturing groups, in the order the groups appear in the pattern.

template<typename S >
QStringView< S > QRegularExpressionMatch< S >::capturedView ( const S &  name) const

Returns the substring captured by the capturing group named name. If the named capturing group did not capture a string or if there is no capturing group matching the specified name, returns an empty string view.

See also
captured(), capturedView(), capturedStart(), capturedEnd(), capturedLength()
template<typename S >
QStringView< S > QRegularExpressionMatch< S >::capturedView ( int  index = 0) const
inline

Returns the substring captured by the index capturing group. If the index capturing group did not capture a string or if there is no such capturing group, returns an empty string view.

See also
captured(), capturedView(), lastCapturedIndex(), capturedStart(), capturedEnd(), capturedLength()
template<typename S >
QStringView< S > QRegularExpressionMatch< S >::capturedView ( QStringView< S >  name) const

Returns the substring captured by the capturing group string view name. If the named capturing group did not capture a string or if there is no capturing group matching the specified name, returns an empty string view.

See also
captured(), capturedView(), capturedStart(), capturedEnd(), capturedLength()
template<typename S >
int QRegularExpressionMatch< S >::captureIndexForName ( const S &  name) const

Returns the index for the capture group with the specified name.

template<typename S >
int QRegularExpressionMatch< S >::captureIndexForName ( QStringView< S >  name) const

Returns the index for the capture group with the specified string view name.

template<typename S >
bool QRegularExpressionMatch< S >::hasMatch ( ) const
inline

Returns true if the regular expression matched the string, or false otherwise.

See also
QRegularExpression::match(), hasPartialMatch()
template<typename S >
bool QRegularExpressionMatch< S >::hasPartialMatch ( ) const
inline

Returns true if the regular expression partially matched the string or false otherwise. If a full match occurred this method will return false while the hasMatch() method will return true.

A partial match will only be attempted if the partial match flag is set in the QRegularExpression.

See also
QRegularExpression::match(), QMatchType, hasMatch()
template<typename S >
bool QRegularExpressionMatch< S >::isValid ( ) const
inline

Returns true if the match object was found as a result of calling QRegularExpression::match(), returns false if the QRegularExpression was invalid.

See also
QRegularExpression::match(), QRegularExpression::isValid()
template<typename S >
int QRegularExpressionMatch< S >::lastCapturedIndex ( ) const
inline

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 captured. If the regular expression did not match, this function returns -1.

Some of the capturing groups with an index less than lastCapturedIndex() may not have matched and therefore captured nothing.

QRegularExpressionMatch match = regExp.match(string);
for (int i = 0; i <= match.lastCapturedIndex(); ++i) {
}
See also
captured(), capturedStart(), capturedEnd(), capturedLength()
template<typename S >
QMatchOptionFlags QRegularExpressionMatch< S >::matchOptions ( ) const

Returns the match options passed to QRegularExpression::match() or QRegularExpression::globalMatch() to obtain this QRegularExpressionMatch object.

See also
QRegularExpression::match(), matchType()
template<typename S >
QMatchType QRegularExpressionMatch< S >::matchType ( ) const

Returns the match type passed to QRegularExpression::match() or QRegularExpression::globalMatch() to obtain this QRegularExpressionMatch object.

See also
QRegularExpression::match(), matchOptions()
template<typename S >
QRegularExpressionMatch & QRegularExpressionMatch< S >::operator= ( const QRegularExpressionMatch< S > &  other)
default

Copy assigns from other and returns a reference to this object.

template<typename S >
QRegularExpressionMatch & QRegularExpressionMatch< S >::operator= ( QRegularExpressionMatch< S > &&  other)
default

Move assigns from other and returns a reference to this object.

template<typename S >
void QRegularExpressionMatch< S >::swap ( QRegularExpressionMatch< S > &  other)
inline

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