Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QRegularExpression Class Reference

\inmodule QtCore \reentrant More...

#include <qregularexpression.h>

+ Collaboration diagram for QRegularExpression:

Public Types

enum  PatternOption {
  NoPatternOption = 0x0000 , CaseInsensitiveOption = 0x0001 , DotMatchesEverythingOption = 0x0002 , MultilineOption = 0x0004 ,
  ExtendedPatternSyntaxOption = 0x0008 , InvertedGreedinessOption = 0x0010 , DontCaptureOption = 0x0020 , UseUnicodePropertiesOption = 0x0040
}
 The PatternOption enum defines modifiers to the way the pattern string should be interpreted, and therefore the way the pattern matches against a subject string. More...
 
enum  MatchType { NormalMatch = 0 , PartialPreferCompleteMatch , PartialPreferFirstMatch , NoMatch }
 The MatchType enum defines the type of the match that should be attempted against the subject string. More...
 
enum  MatchOption { NoMatchOption = 0x0000 , AnchorAtOffsetMatchOption = 0x0001 , Q_DECL_ENUMERATOR_DEPRECATED_X , DontCheckSubjectStringMatchOption = 0x0002 }
 \value NoMatchOption No match options are set. More...
 
enum  WildcardConversionOption { DefaultWildcardConversion = 0x0 , UnanchoredWildcardConversion = 0x1 , NonPathWildcardConversion = 0x2 }
 

Public Member Functions

PatternOptions patternOptions () const
 Returns the pattern options for the regular expression.
 
void setPatternOptions (PatternOptions options)
 Sets the given options as the pattern options of the regular expression.
 
 QRegularExpression ()
 Constructs a QRegularExpression object with an empty pattern and no pattern options.
 
 QRegularExpression (const QString &pattern, PatternOptions options=NoPatternOption)
 Constructs a QRegularExpression object using the given pattern as pattern and the options as the pattern options.
 
 QRegularExpression (const QRegularExpression &re) noexcept
 Constructs a QRegularExpression object as a copy of re.
 
 QRegularExpression (QRegularExpression &&re)=default
 
 ~QRegularExpression ()
 Destroys the QRegularExpression object.
 
QRegularExpressionoperator= (const QRegularExpression &re) noexcept
 Assigns the regular expression re to this object, and returns a reference to the copy.
 
void swap (QRegularExpression &other) noexcept
 Swaps the regular expression other with this regular expression.
 
QString pattern () const
 Returns the pattern string of the regular expression.
 
void setPattern (const QString &pattern)
 Sets the pattern string of the regular expression to pattern.
 
bool isValid () const
 Returns true if the regular expression is a valid regular expression (that is, it contains no syntax errors, etc.), or false otherwise.
 
qsizetype patternErrorOffset () const
 Returns the offset, inside the pattern string, at which an error was found when checking the validity of the regular expression.
 
QString errorString () const
 Returns a textual description of the error found when checking the validity of the regular expression, or "no error" if no error was found.
 
int captureCount () const
 Returns the number of capturing groups inside the pattern string, or -1 if the regular expression is not valid.
 
QStringList namedCaptureGroups () const
 
QRegularExpressionMatch match (const QString &subject, qsizetype offset=0, MatchType matchType=NormalMatch, MatchOptions matchOptions=NoMatchOption) const
 Attempts to match the regular expression against the given subject string, starting at the position offset inside the subject, using a match of type matchType and honoring the given matchOptions.
 
QRegularExpressionMatch matchView (QStringView subjectView, qsizetype offset=0, MatchType matchType=NormalMatch, MatchOptions matchOptions=NoMatchOption) const
 
QRegularExpressionMatchIterator globalMatch (const QString &subject, qsizetype offset=0, MatchType matchType=NormalMatch, MatchOptions matchOptions=NoMatchOption) const
 Attempts to perform a global match of the regular expression against the given subject string, starting at the position offset inside the subject, using a match of type matchType and honoring the given matchOptions.
 
QRegularExpressionMatchIterator globalMatchView (QStringView subjectView, qsizetype offset=0, MatchType matchType=NormalMatch, MatchOptions matchOptions=NoMatchOption) const
 
void optimize () const
 

Static Public Member Functions

static QString escape (const QString &str)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
static QString wildcardToRegularExpression (const QString &str, WildcardConversionOptions options=DefaultWildcardConversion)
 
static QString anchoredPattern (const QString &expression)
 
static QString escape (QStringView str)
 
static QString wildcardToRegularExpression (QStringView str, WildcardConversionOptions options=DefaultWildcardConversion)
 
static QString anchoredPattern (QStringView expression)
 
static QRegularExpression fromWildcard (QStringView pattern, Qt::CaseSensitivity cs=Qt::CaseInsensitive, WildcardConversionOptions options=DefaultWildcardConversion)
 

Friends

struct QRegularExpressionPrivate
 
class QRegularExpressionMatch
 
struct QRegularExpressionMatchPrivate
 
class QRegularExpressionMatchIterator
 
Q_CORE_EXPORT bool comparesEqual (const QRegularExpression &lhs, const QRegularExpression &rhs) noexcept
 
Q_CORE_EXPORT size_t qHash (const QRegularExpression &key, size_t seed) noexcept
 

Related Symbols

(Note that these are not member symbols.)

size_t qHash (const QRegularExpression &key, size_t seed) noexcept
 
QDataStreamoperator<< (QDataStream &out, const QRegularExpression &re)
 Writes the regular expression re to stream out.
 
QDataStreamoperator>> (QDataStream &in, QRegularExpression &re)
 Reads a regular expression from stream in into re.
 
QDebug operator<< (QDebug debug, const QRegularExpression &re)
 Writes the regular expression re into the debug object debug for debugging purposes.
 
QDebug operator<< (QDebug debug, QRegularExpression::PatternOptions patternOptions)
 Writes the pattern options patternOptions into the debug object debug for debugging purposes.
 

Detailed Description

\inmodule QtCore \reentrant

The QRegularExpression class provides pattern matching using regular expressions.

Since
5.0

\keyword regular expression

\compares equality Regular expressions, or {regexps}, are a very powerful tool to handle strings and texts. This is useful in many contexts, e.g.,

\table \row

  • Validation
  • A regexp can test whether a substring meets some criteria, e.g. is an integer or contains no whitespace. \row
  • Searching
  • A regexp provides more powerful pattern matching than simple substring matching, e.g., match one of the words {mail}, {letter} or {correspondence}, but none of the words {email}, {mailman}, {mailer}, {letterbox}, etc. \row
  • Search and Replace
  • A regexp can replace all occurrences of a substring with a different substring, e.g., replace all occurrences of {&} with {&amp;} except where the {&} is already followed by an {amp;}. \row
  • String Splitting
  • A regexp can be used to identify where a string should be split apart, e.g. splitting tab-delimited strings. \endtable

This document is by no means a complete reference to pattern matching using regular expressions, and the following parts will require the reader to have some basic knowledge about Perl-like regular expressions and their pattern syntax.

Good references about regular expressions include:

\list

Definition at line 30 of file qregularexpression.h.

Member Enumeration Documentation

◆ MatchOption

\value NoMatchOption No match options are set.

\value AnchoredMatchOption Use AnchorAtOffsetMatchOption instead.

\value AnchorAtOffsetMatchOption The match is constrained to start exactly at the offset passed to match() in order to be successful, even if the pattern string does not contain any metacharacter that anchors the match at that point. Note that passing this option does not anchor the end of the match to the end of the subject; if you want to fully anchor a regular expression, use anchoredPattern(). This enum value has been introduced in Qt 6.0.

\value DontCheckSubjectStringMatchOption The subject string is not checked for UTF-16 validity before attempting a match. Use this option with extreme caution, as attempting to match an invalid string may crash the program and/or constitute a security issue. This enum value has been introduced in Qt 5.4.

Enumerator
NoMatchOption 
AnchorAtOffsetMatchOption 
Q_DECL_ENUMERATOR_DEPRECATED_X 
DontCheckSubjectStringMatchOption 

Definition at line 79 of file qregularexpression.h.

◆ MatchType

The MatchType enum defines the type of the match that should be attempted against the subject string.

\value NormalMatch A normal match is done.

\value PartialPreferCompleteMatch The pattern string is matched partially against the subject string. If a partial match is found, then it is recorded, and other matching alternatives are tried as usual. If a complete match is then found, then it's preferred to the partial match; in this case only the complete match is reported. If instead no complete match is found (but only the partial one), then the partial one is reported.

\value PartialPreferFirstMatch The pattern string is matched partially against the subject string. If a partial match is found, then matching stops and the partial match is reported. In this case, other matching alternatives (potentially leading to a complete match) are not tried. Moreover, this match type assumes that the subject string only a substring of a larger text, and that (in this text) there are other characters beyond the end of the subject string. This can lead to surprising results; see the discussion in the \l{partial matching} section for more details.

\value NoMatch No matching is done. This value is returned as the match type by a default constructed QRegularExpressionMatch or QRegularExpressionMatchIterator. Using this match type is not very useful for the user, as no matching ever happens. This enum value has been introduced in Qt 5.1.

Enumerator
NormalMatch 
PartialPreferCompleteMatch 
PartialPreferFirstMatch 
NoMatch 

Definition at line 72 of file qregularexpression.h.

◆ PatternOption

The PatternOption enum defines modifiers to the way the pattern string should be interpreted, and therefore the way the pattern matches against a subject string.

\value NoPatternOption No pattern options are set.

\value CaseInsensitiveOption The pattern should match against the subject string in a case insensitive way. This option corresponds to the /i modifier in Perl regular expressions.

\value DotMatchesEverythingOption The dot metacharacter ({.}) in the pattern string is allowed to match any character in the subject string, including newlines (normally, the dot does not match newlines). This option corresponds to the {/s} modifier in Perl regular expressions.

\value MultilineOption The caret ({^}) and the dollar ({$}) metacharacters in the pattern string are allowed to match, respectively, immediately after and immediately before any newline in the subject string, as well as at the very beginning and at the very end of the subject string. This option corresponds to the {/m} modifier in Perl regular expressions.

\value ExtendedPatternSyntaxOption Any whitespace in the pattern string which is not escaped and outside a character class is ignored. Moreover, an unescaped sharp ({#}) outside a character class causes all the following characters, until the first newline (included), to be ignored. This can be used to increase the readability of a pattern string as well as put comments inside regular expressions; this is particularly useful if the pattern string is loaded from a file or written by the user, because in C++ code it is always possible to use the rules for string literals to put comments outside the pattern string. This option corresponds to the {/x} modifier in Perl regular expressions.

\value InvertedGreedinessOption The greediness of the quantifiers is inverted: {*}, {+}, {?}, {{m,n}}, etc. become lazy, while their lazy versions ({*?}, {+?}, {??}, {{m,n}?}, etc.) become greedy. There is no equivalent for this option in Perl regular expressions.

\value DontCaptureOption The non-named capturing groups do not capture substrings; named capturing groups still work as intended, as well as the implicit capturing group number 0 corresponding to the entire match. There is no equivalent for this option in Perl regular expressions.

\value UseUnicodePropertiesOption The meaning of the {\w}, {\d}, etc., character classes, as well as the meaning of their counterparts ({\W}, {\D}, etc.), is changed from matching ASCII characters only to matching any character with the corresponding Unicode property. For instance, {\d} is changed to match any character with the Unicode Nd (decimal digit) property; {\w} to match any character with either the Unicode L (letter) or N (digit) property, plus underscore, and so on. This option corresponds to the {/u} modifier in Perl regular expressions.

Enumerator
NoPatternOption 
CaseInsensitiveOption 
DotMatchesEverythingOption 
MultilineOption 
ExtendedPatternSyntaxOption 
InvertedGreedinessOption 
DontCaptureOption 
UseUnicodePropertiesOption 

Definition at line 33 of file qregularexpression.h.

◆ WildcardConversionOption

Since
6.0

The WildcardConversionOption enum defines modifiers to the way a wildcard glob pattern gets converted to a regular expression pattern.

\value DefaultWildcardConversion No conversion options are set.

\value UnanchoredWildcardConversion The conversion will not anchor the pattern. This allows for partial string matches of wildcard expressions.

\value [since 6.6] NonPathWildcardConversion The conversion will {not} interpret the pattern as filepath globbing.

See also
QRegularExpression::wildcardToRegularExpression
Enumerator
DefaultWildcardConversion 
UnanchoredWildcardConversion 
NonPathWildcardConversion 

Definition at line 132 of file qregularexpression.h.

Constructor & Destructor Documentation

◆ QRegularExpression() [1/4]

QRegularExpression::QRegularExpression ( )

Constructs a QRegularExpression object with an empty pattern and no pattern options.

See also
setPattern(), setPatternOptions()

Definition at line 1335 of file qregularexpression.cpp.

Referenced by fromWildcard().

+ Here is the caller graph for this function:

◆ QRegularExpression() [2/4]

QRegularExpression::QRegularExpression ( const QString & pattern,
PatternOptions options = NoPatternOption )
explicit

Constructs a QRegularExpression object using the given pattern as pattern and the options as the pattern options.

See also
setPattern(), setPatternOptions()

Definition at line 1346 of file qregularexpression.cpp.

References QRegularExpressionPrivate::pattern, pattern(), and QRegularExpressionPrivate::patternOptions.

+ Here is the call graph for this function:

◆ QRegularExpression() [3/4]

QRegularExpression::QRegularExpression ( const QRegularExpression & re)
defaultnoexcept

Constructs a QRegularExpression object as a copy of re.

See also
operator=()

◆ QRegularExpression() [4/4]

QRegularExpression::QRegularExpression ( QRegularExpression && re)
default
Since
6.1

Constructs a QRegularExpression object by moving from re.

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

See also
operator=()

◆ ~QRegularExpression()

QRegularExpression::~QRegularExpression ( )

Destroys the QRegularExpression object.

Definition at line 1377 of file qregularexpression.cpp.

Member Function Documentation

◆ anchoredPattern() [1/2]

QRegularExpression::anchoredPattern ( const QString & expression)
inlinestatic
Since
5.12 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 149 of file qregularexpression.h.

References anchoredPattern(), and qToStringViewIgnoringNull().

Referenced by QQmlJS::Dom::FieldFilter::addFilter(), QQmlJS::Dom::QmlDirectory::addQmlFilePath(), anchoredPattern(), QQmlJS::Dom::DomUniverse::doCopy(), QSslCertificate::fromPath(), LocalFileApi::Type::Accept::MimeType::Extension::fromQt(), QQmlJS::Dom::Version::fromString(), QQmlJS::Dom::Import::importRe(), QQuickComboBoxPrivate::match(), QQuickDropArea::setKeys(), QQmlJS::Dom::QQmlDomAstCreator::visit(), and wildcardToRegularExpression().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ anchoredPattern() [2/2]

QString QRegularExpression::anchoredPattern ( QStringView expression)
static
Since
5.15

Returns the expression wrapped between the {\A} and {\z} anchors to be used for exact matching.

Definition at line 2069 of file qregularexpression.cpp.

◆ captureCount()

int QRegularExpression::captureCount ( ) const

Returns the number of capturing groups inside the pattern string, or -1 if the regular expression is not valid.

Note
The implicit capturing group 0 is {not} included in the returned number.
See also
isValid()

Definition at line 1454 of file qregularexpression.cpp.

References QRegularExpressionPrivate::capturingCount, and isValid().

+ Here is the call graph for this function:

◆ errorString()

QString QRegularExpression::errorString ( ) const

Returns a textual description of the error found when checking the validity of the regular expression, or "no error" if no error was found.

See also
isValid(), patternErrorOffset()

Definition at line 1537 of file qregularexpression.cpp.

References QRegularExpressionPrivate::compilePattern(), QString::data(), QExplicitlySharedDataPointer< T >::data(), QRegularExpressionPrivate::errorCode, errorString(), QString::resize(), QString::size(), and QCoreApplication::translate().

Referenced by errorString().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ escape() [1/2]

QString QRegularExpression::escape ( const QString & str)
inlinestatic

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 139 of file qregularexpression.h.

References escape(), qToStringViewIgnoringNull(), and str.

Referenced by findStyleInModel(), main(), QSortFilterProxyModel::setFilterFixedString(), and QQuickDropArea::setKeys().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ escape() [2/2]

QString QRegularExpression::escape ( QStringView str)
static
Since
5.15

Escapes all characters of str so that they no longer have any special meaning when used as a regular expression pattern string, and returns the escaped string. For instance:

QString escaped = QRegularExpression::escape("a(x) = f(x) + g(x)");
// escaped == "a\\‍(x\\‍)\\ \\=\\ f\\‍(x\\‍)\\ \\+\\ g\\‍(x\\‍)"

This is very convenient in order to build patterns from arbitrary strings:

Note
This function implements Perl's quotemeta algorithm and escapes with a backslash all characters in str, except for the characters in the {[A-Z]}, {[a-z]} and {[0-9]} ranges, as well as the underscore ({_}) character. The only difference with Perl is that a literal NUL inside str is escaped with the sequence {"\\0"} (backslash + {'0'}), instead of {"\\\0"} (backslash + {NUL}).

Definition at line 1809 of file qregularexpression.cpp.

References QString::at(), i, QString::size(), and str.

+ Here is the call graph for this function:

◆ fromWildcard()

QRegularExpression QRegularExpression::fromWildcard ( QStringView pattern,
Qt::CaseSensitivity cs = Qt::CaseInsensitive,
WildcardConversionOptions options = DefaultWildcardConversion )
static
Since
6.0 Returns a regular expression of the glob pattern pattern. The regular expression will be case sensitive if cs is \l{Qt::CaseSensitive}, and converted according to options.

Equivalent to

return QRegularExpression(wildcardToRegularExpression(str, options), reOptions);
static QString wildcardToRegularExpression(const QString &str, WildcardConversionOptions options=DefaultWildcardConversion)
QRegularExpression()
Constructs a QRegularExpression object with an empty pattern and no pattern options.
QString str
[2]
@ CaseSensitive

Definition at line 2049 of file qregularexpression.cpp.

References QRegularExpression(), CaseInsensitiveOption, Qt::CaseSensitive, NoPatternOption, and wildcardToRegularExpression().

Referenced by QDirListingPrivate::init(), isBypassed(), isHostExcluded(), and QMimeGlobPattern::matchFileName().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ globalMatch()

QRegularExpressionMatchIterator QRegularExpression::globalMatch ( const QString & subject,
qsizetype offset = 0,
MatchType matchType = NormalMatch,
MatchOptions matchOptions = NoMatchOption ) const

Attempts to perform a global match of the regular expression against the given subject string, starting at the position offset inside the subject, using a match of type matchType and honoring the given matchOptions.

The returned QRegularExpressionMatchIterator is positioned before the first match result (if any).

See also
QRegularExpressionMatchIterator, {global matching}

Definition at line 1661 of file qregularexpression.cpp.

References match(), priv(), and QRegularExpressionMatchIterator.

Referenced by src_gui_text_qsyntaxhighlighter::MyHighlighter::highlightBlock().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ globalMatchView()

QRegularExpressionMatchIterator QRegularExpression::globalMatchView ( QStringView subjectView,
qsizetype offset = 0,
MatchType matchType = NormalMatch,
MatchOptions matchOptions = NoMatchOption ) const
Since
6.5 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Attempts to perform a global match of the regular expression against the given subjectView string view, starting at the position offset inside the subject, using a match of type matchType and honoring the given matchOptions.

The returned QRegularExpressionMatchIterator is positioned before the first match result (if any).

Note
The data referenced by subjectView must remain valid as long as there are QRegularExpressionMatchIterator or QRegularExpressionMatch objects using it.
See also
QRegularExpressionMatchIterator, {global matching}

Definition at line 1710 of file qregularexpression.cpp.

References matchView(), priv(), and QRegularExpressionMatchIterator.

+ Here is the call graph for this function:

◆ isValid()

bool QRegularExpression::isValid ( ) const

Returns true if the regular expression is a valid regular expression (that is, it contains no syntax errors, etc.), or false otherwise.

Use errorString() to obtain a textual description of the error.

See also
errorString(), patternErrorOffset()

Definition at line 1525 of file qregularexpression.cpp.

References QRegularExpressionPrivate::compiledPattern, QRegularExpressionPrivate::compilePattern(), and QExplicitlySharedDataPointer< T >::data().

Referenced by captureCount(), and namedCaptureGroups().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ match()

QRegularExpressionMatch QRegularExpression::match ( const QString & subject,
qsizetype offset = 0,
MatchType matchType = NormalMatch,
MatchOptions matchOptions = NoMatchOption ) const

Attempts to match the regular expression against the given subject string, starting at the position offset inside the subject, using a match of type matchType and honoring the given matchOptions.

The returned QRegularExpressionMatch object contains the results of the match.

See also
QRegularExpressionMatch, {normal matching}

Definition at line 1587 of file qregularexpression.cpp.

References QRegularExpressionPrivate::compilePattern(), QExplicitlySharedDataPointer< T >::data(), QRegularExpressionPrivate::doMatch(), priv(), QRegularExpressionMatch, and QRegularExpressionMatchPrivate.

Referenced by globalMatch(), QOCIDriver::open(), and QDomDocumentPrivate::saveDocument().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ matchView()

QRegularExpressionMatch QRegularExpression::matchView ( QStringView subjectView,
qsizetype offset = 0,
MatchType matchType = NormalMatch,
MatchOptions matchOptions = NoMatchOption ) const
Since
6.5 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Attempts to match the regular expression against the given subjectView string view, starting at the position offset inside the subject, using a match of type matchType and honoring the given matchOptions.

The returned QRegularExpressionMatch object contains the results of the match.

Note
The data referenced by subjectView must remain valid as long as there are QRegularExpressionMatch objects using it.
See also
QRegularExpressionMatch, {normal matching}

Definition at line 1635 of file qregularexpression.cpp.

References QRegularExpressionPrivate::compilePattern(), QExplicitlySharedDataPointer< T >::data(), QRegularExpressionPrivate::doMatch(), priv(), QRegularExpressionMatch, and QRegularExpressionMatchPrivate.

Referenced by globalMatchView().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ namedCaptureGroups()

QStringList QRegularExpression::namedCaptureGroups ( ) const
Since
5.1

Returns a list of captureCount() + 1 elements, containing the names of the named capturing groups in the pattern string. The list is sorted such that the element of the list at position {i} is the name of the {i}-th capturing group, if it has a name, or an empty string if that capturing group is unnamed.

For instance, given the regular expression

namedCaptureGroups() will return the following list:

which corresponds to the fact that the capturing group #0 (corresponding to the whole match) has no name, the capturing group #1 has name "day", the capturing group #2 has name "month", etc.

If the regular expression is not valid, returns an empty list.

See also
isValid(), QRegularExpressionMatch::captured(), QString::isEmpty()

Definition at line 1486 of file qregularexpression.cpp.

References QRegularExpressionPrivate::capturingCount, QRegularExpressionPrivate::compiledPattern, i, isValid(), and QStringView::toString().

+ Here is the call graph for this function:

◆ operator=()

QRegularExpression & QRegularExpression::operator= ( const QRegularExpression & re)
defaultnoexcept

Assigns the regular expression re to this object, and returns a reference to the copy.

Move-assigns the regular expression re to this object, and returns a reference to the result.

Both the pattern and the pattern options are copied.

Both the pattern and the pattern options are copied.

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

◆ optimize()

void QRegularExpression::optimize ( ) const
Since
5.4

Compiles the pattern immediately, including JIT compiling it (if the JIT is enabled) for optimization.

See also
isValid(), {Debugging Code that Uses QRegularExpression}

Definition at line 1732 of file qregularexpression.cpp.

References QRegularExpressionPrivate::compilePattern(), and QExplicitlySharedDataPointer< T >::data().

+ Here is the call graph for this function:

◆ pattern()

QString QRegularExpression::pattern ( ) const

Returns the pattern string of the regular expression.

See also
setPattern(), patternOptions()

Definition at line 1401 of file qregularexpression.cpp.

References QRegularExpressionPrivate::pattern.

Referenced by QRegularExpression(), QQuickDropAreaPrivate::hasMatchingKey(), and setPattern().

+ Here is the caller graph for this function:

◆ patternErrorOffset()

qsizetype QRegularExpression::patternErrorOffset ( ) const

Returns the offset, inside the pattern string, at which an error was found when checking the validity of the regular expression.

If no error was found, then -1 is returned.

See also
pattern(), isValid(), errorString()

Definition at line 1571 of file qregularexpression.cpp.

References QRegularExpressionPrivate::compilePattern(), QExplicitlySharedDataPointer< T >::data(), and QRegularExpressionPrivate::errorOffset.

+ Here is the call graph for this function:

◆ patternOptions()

QRegularExpression::PatternOptions QRegularExpression::patternOptions ( ) const

Returns the pattern options for the regular expression.

See also
setPatternOptions(), pattern()

Definition at line 1426 of file qregularexpression.cpp.

References QRegularExpressionPrivate::patternOptions.

Referenced by QSortFilterProxyModel::setFilterRegularExpression().

+ Here is the caller graph for this function:

◆ setPattern()

void QRegularExpression::setPattern ( const QString & pattern)

Sets the pattern string of the regular expression to pattern.

The pattern options are left unchanged.

See also
pattern(), setPatternOptions()

Definition at line 1412 of file qregularexpression.cpp.

References QExplicitlySharedDataPointer< T >::detach(), QRegularExpressionPrivate::isDirty, QRegularExpressionPrivate::pattern, and pattern().

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setPatternOptions()

void QRegularExpression::setPatternOptions ( PatternOptions options)

Sets the given options as the pattern options of the regular expression.

The pattern string is left unchanged.

See also
patternOptions(), setPattern()

Definition at line 1437 of file qregularexpression.cpp.

References QExplicitlySharedDataPointer< T >::detach(), QRegularExpressionPrivate::isDirty, and QRegularExpressionPrivate::patternOptions.

Referenced by QSortFilterProxyModel::setFilterCaseSensitivity().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ swap()

void QRegularExpression::swap ( QRegularExpression & other)
inlinenoexcept

Swaps the regular expression other with this regular expression.

This operation is very fast and never fails.

Definition at line 59 of file qregularexpression.h.

References d, and other().

+ Here is the call graph for this function:

◆ wildcardToRegularExpression() [1/2]

QString QRegularExpression::wildcardToRegularExpression ( const QString & pattern,
WildcardConversionOptions options = DefaultWildcardConversion )
inlinestatic
Since
5.12

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 144 of file qregularexpression.h.

References qToStringViewIgnoringNull(), str, and wildcardToRegularExpression().

Referenced by QSslCertificate::fromPath(), fromWildcard(), main(), QAbstractItemModel::match(), QQuickComboBoxPrivate::match(), QSortFilterProxyModel::setFilterWildcard(), and wildcardToRegularExpression().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ wildcardToRegularExpression() [2/2]

QString QRegularExpression::wildcardToRegularExpression ( QStringView pattern,
WildcardConversionOptions options = DefaultWildcardConversion )
static
Since
5.15

Returns a regular expression representation of the given glob pattern.

There are two transformations possible, one that targets file path globbing, and another one which is more generic.

By default, the transformation is targeting file path globbing, which means in particular that path separators receive special treatment. This implies that it is not just a basic translation from "*" to ".*" and similar.

// Will match files with names like:
// foo.jpeg
// f_o_o.jpeg
// föö.jpeg

The more generic globbing transformation is available by passing NonPathWildcardConversion in the conversion options.

This implementation follows closely the definition of wildcard for glob patterns: \table \row

  • {c}
  • Any character represents itself apart from those mentioned below. Thus {c} matches the character c. \row
  • {?}
  • Matches any single character, except for a path separator (in case file path globbing has been selected). It is the same as b{.} in full regexps. \row
  • {*}
  • Matches zero or more of any characters, except for path separators (in case file path globbing has been selected). It is the same as {.*} in full regexps. \row
  • {[abc]}
  • Matches one character given in the bracket. \row
  • {[a-c]}
  • Matches one character from the range given in the bracket. \row
  • {[!abc]}
  • Matches one character that is not given in the bracket. It is the same as {[^abc]} in full regexp. \row
  • {[!a-c]}
  • Matches one character that is not from the range given in the bracket. It is the same as {[^a-c]} in full regexp. \endtable
Note
For historical reasons, a backslash (\) character is not an escape char in this context. In order to match one of the special characters, place it in square brackets (for example, {[?]}).

More information about the implementation can be found in: \list

By default, the returned regular expression is fully anchored. In other words, there is no need of calling anchoredPattern() again on the result. To get a regular expression that is not anchored, pass UnanchoredWildcardConversion in the conversion options.

See also
escape()

Definition at line 1931 of file qregularexpression.cpp.

References anchoredPattern(), i, NonPathWildcardConversion, rx(), settings, and UnanchoredWildcardConversion.

+ Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ comparesEqual

Q_CORE_EXPORT bool comparesEqual ( const QRegularExpression & lhs,
const QRegularExpression & rhs )
friend

Definition at line 1746 of file qregularexpression.cpp.

◆ operator<<() [1/3]

QDataStream & operator<< ( QDataStream & out,
const QRegularExpression & re )
related

Writes the regular expression re to stream out.

See also
{Serializing Qt Data Types}

Definition at line 2751 of file qregularexpression.cpp.

References out.

◆ operator<<() [2/3]

QDebug operator<< ( QDebug debug,
const QRegularExpression & re )
related

Writes the regular expression re into the debug object debug for debugging purposes.

See also
{Debugging Techniques}

Definition at line 2784 of file qregularexpression.cpp.

References debug.

◆ operator<<() [3/3]

QDebug operator<< ( QDebug debug,
QRegularExpression::PatternOptions patternOptions )
related

Writes the pattern options patternOptions into the debug object debug for debugging purposes.

See also
{Debugging Techniques}

Definition at line 2799 of file qregularexpression.cpp.

References CaseInsensitiveOption, debug, DontCaptureOption, DotMatchesEverythingOption, ExtendedPatternSyntaxOption, InvertedGreedinessOption, MultilineOption, NoPatternOption, QByteArray::reserve(), and UseUnicodePropertiesOption.

+ Here is the call graph for this function:

◆ operator>>()

QDataStream & operator>> ( QDataStream & in,
QRegularExpression & re )
related

Reads a regular expression from stream in into re.

See also
{Serializing Qt Data Types}

Definition at line 2764 of file qregularexpression.cpp.

◆ qHash() [1/2]

size_t qHash ( const QRegularExpression & key,
size_t seed )
related
Since
5.6

Returns the hash value for key, using seed to seed the calculation.

Definition at line 1779 of file qregularexpression.cpp.

◆ qHash [2/2]

size_t qHash ( const QRegularExpression & key,
size_t seed = 0 )
friend
Since
5.6

Returns the hash value for key, using seed to seed the calculation.

Definition at line 1779 of file qregularexpression.cpp.

◆ QRegularExpressionMatch

friend class QRegularExpressionMatch
friend

Definition at line 170 of file qregularexpression.h.

Referenced by match(), and matchView().

◆ QRegularExpressionMatchIterator

friend class QRegularExpressionMatchIterator
friend

Definition at line 172 of file qregularexpression.h.

Referenced by globalMatch(), and globalMatchView().

◆ QRegularExpressionMatchPrivate

friend struct QRegularExpressionMatchPrivate
friend

Definition at line 171 of file qregularexpression.h.

Referenced by match(), and matchView().

◆ QRegularExpressionPrivate

friend struct QRegularExpressionPrivate
friend

Definition at line 169 of file qregularexpression.h.


The documentation for this class was generated from the following files: