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.h
Go to the documentation of this file.
1// Copyright (C) 2020 Giuseppe D'Angelo <dangelog@gmail.com>.
2// Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
3// Copyright (C) 2021 The Qt Company Ltd.
4// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
5
6#ifndef QREGULAREXPRESSION_H
7#define QREGULAREXPRESSION_H
8
9#include <QtCore/qglobal.h>
10#include <QtCore/qstring.h>
11#include <QtCore/qstringview.h>
12#include <QtCore/qshareddata.h>
13#include <QtCore/qvariant.h>
14
15#include <iterator>
16
17QT_REQUIRE_CONFIG(regularexpression);
18
20
25
27
28Q_CORE_EXPORT size_t qHash(const QRegularExpression &key, size_t seed = 0) noexcept;
29
30class Q_CORE_EXPORT QRegularExpression
31{
32public:
42 // Formerly (no-ops deprecated in 5.12, removed 6.0):
43 // OptimizeOnFirstUsageOption = 0x0080,
44 // DontAutomaticallyOptimizeOption = 0x0100,
45 };
46 Q_DECLARE_FLAGS(PatternOptions, PatternOption)
47
48 PatternOptions patternOptions() const;
49 void setPatternOptions(PatternOptions options);
50
52 explicit QRegularExpression(const QString &pattern, PatternOptions options = NoPatternOption);
57 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QRegularExpression)
58
59 void swap(QRegularExpression &other) noexcept { d.swap(other.d); }
60
61 QString pattern() const;
62 void setPattern(const QString &pattern);
63
64 [[nodiscard]]
65 bool isValid() const;
67 QString errorString() const;
68
69 int captureCount() const;
71
78
80 NoMatchOption = 0x0000,
82 AnchoredMatchOption Q_DECL_ENUMERATOR_DEPRECATED_X(
83 "Use AnchorAtOffsetMatchOption instead") = AnchorAtOffsetMatchOption, // Rename@Qt6.0
85 };
86 Q_DECLARE_FLAGS(MatchOptions, MatchOption)
87
88 [[nodiscard]]
90 qsizetype offset = 0,
91 MatchType matchType = NormalMatch,
92 MatchOptions matchOptions = NoMatchOption) const;
93
94#if QT_DEPRECATED_SINCE(6, 8)
95 [[nodiscard]]
96 QT_DEPRECATED_VERSION_X_6_8("Use matchView instead.")
98 qsizetype offset = 0,
99 MatchType matchType = NormalMatch,
100 MatchOptions matchOptions = NoMatchOption) const;
101#endif
102
103 [[nodiscard]]
105 qsizetype offset = 0,
106 MatchType matchType = NormalMatch,
107 MatchOptions matchOptions = NoMatchOption) const;
108
109 [[nodiscard]]
111 qsizetype offset = 0,
112 MatchType matchType = NormalMatch,
113 MatchOptions matchOptions = NoMatchOption) const;
114
115#if QT_DEPRECATED_SINCE(6, 8)
116 [[nodiscard]]
117 QT_DEPRECATED_VERSION_X_6_8("Use globalMatchView instead.")
119 qsizetype offset = 0,
120 MatchType matchType = NormalMatch,
121 MatchOptions matchOptions = NoMatchOption) const;
122#endif
123
124 [[nodiscard]]
126 qsizetype offset = 0,
127 MatchType matchType = NormalMatch,
128 MatchOptions matchOptions = NoMatchOption) const;
129
130 void optimize() const;
131
137 Q_DECLARE_FLAGS(WildcardConversionOptions, WildcardConversionOption)
138
139 static QString escape(const QString &str)
140 {
142 }
143
144 static QString wildcardToRegularExpression(const QString &str, WildcardConversionOptions options = DefaultWildcardConversion)
145 {
147 }
148
149 static inline QString anchoredPattern(const QString &expression)
150 {
151 return anchoredPattern(qToStringViewIgnoringNull(expression));
152 }
153
155 static QString wildcardToRegularExpression(QStringView str, WildcardConversionOptions options = DefaultWildcardConversion);
156 static QString anchoredPattern(QStringView expression);
157
159 WildcardConversionOptions options = DefaultWildcardConversion);
160#if QT_CORE_REMOVED_SINCE(6, 8)
161 bool operator==(const QRegularExpression &re) const;
162 inline bool operator!=(const QRegularExpression &re) const { return !operator==(re); }
163#endif
164private:
165 friend Q_CORE_EXPORT bool comparesEqual(const QRegularExpression &lhs,
166 const QRegularExpression &rhs) noexcept;
168
173 friend Q_CORE_EXPORT size_t qHash(const QRegularExpression &key, size_t seed) noexcept;
174
177};
178
179Q_DECLARE_SHARED(QRegularExpression)
182Q_DECLARE_OPERATORS_FOR_FLAGS(QRegularExpression::WildcardConversionOptions)
183
184#ifndef QT_NO_DATASTREAM
187#endif
188
189#ifndef QT_NO_DEBUG_STREAM
191Q_CORE_EXPORT QDebug operator<<(QDebug debug, QRegularExpression::PatternOptions patternOptions);
192#endif
193
196
197class Q_CORE_EXPORT QRegularExpressionMatch
198{
199public:
207 void swap(QRegularExpressionMatch &other) noexcept { d.swap(other.d); }
208
211 QRegularExpression::MatchOptions matchOptions() const;
212
213 bool hasMatch() const;
214 bool hasPartialMatch() const;
215
216 bool isValid() const;
217
218 int lastCapturedIndex() const;
219
220#if QT_CORE_REMOVED_SINCE(6, 8)
221 bool hasCaptured(const QString &name) const
222 { return hasCaptured(qToAnyStringViewIgnoringNull(name)); }
223 bool hasCaptured(QStringView name) const;
224#endif
225 bool hasCaptured(QAnyStringView name) const;
226 bool hasCaptured(int nth) const;
227
228 QString captured(int nth = 0) const;
229 QStringView capturedView(int nth = 0) const;
230
231#if QT_CORE_REMOVED_SINCE(6, 8)
232 QString captured(const QString &name) const
233 { return captured(qToAnyStringViewIgnoringNull(name)); }
234
235 QString captured(QStringView name) const;
236 QStringView capturedView(QStringView name) const;
237#endif
238 QString captured(QAnyStringView name) const;
239 QStringView capturedView(QAnyStringView name) const;
240
241 QStringList capturedTexts() const;
242
243 qsizetype capturedStart(int nth = 0) const;
244 qsizetype capturedLength(int nth = 0) const;
245 qsizetype capturedEnd(int nth = 0) const;
246
247#if QT_CORE_REMOVED_SINCE(6, 8)
248 qsizetype capturedStart(const QString &name) const
249 { return capturedStart(qToAnyStringViewIgnoringNull(name)); }
250 qsizetype capturedLength(const QString &name) const
251 { return capturedLength(qToAnyStringViewIgnoringNull(name)); }
252 qsizetype capturedEnd(const QString &name) const
253 { return capturedEnd(qToAnyStringViewIgnoringNull(name)); }
254
255 qsizetype capturedStart(QStringView name) const;
256 qsizetype capturedLength(QStringView name) const;
257 qsizetype capturedEnd(QStringView name) const;
258#endif
259 qsizetype capturedStart(QAnyStringView name) const;
260 qsizetype capturedLength(QAnyStringView name) const;
261 qsizetype capturedEnd(QAnyStringView name) const;
262
263private:
264 friend class QRegularExpression;
267
269 QExplicitlySharedDataPointer<QRegularExpressionMatchPrivate> d;
270};
271
272Q_DECLARE_SHARED(QRegularExpressionMatch)
273
274#ifndef QT_NO_DEBUG_STREAM
276#endif
277
278namespace QtPrivate {
279class QRegularExpressionMatchIteratorRangeBasedForIterator;
281}
282
285
287{
288public:
295 { d.swap(iterator.d); return *this; }
296 void swap(QRegularExpressionMatchIterator &other) noexcept { d.swap(other.d); }
297
298 bool isValid() const;
299
300 bool hasNext() const;
302 QRegularExpressionMatch peekNext() const;
303
306 QRegularExpression::MatchOptions matchOptions() const;
307
308private:
309 friend class QRegularExpression;
312
314 QExplicitlySharedDataPointer<QRegularExpressionMatchIteratorPrivate> d;
315};
316
317namespace QtPrivate {
318
319// support for range-based for loop
321{
322public:
324 using difference_type = int;
327 using iterator_category = std::forward_iterator_tag;
328
333
335 : m_matchIterator(iterator),
336 m_currentMatch(),
337 m_atEnd(false)
338 {
339 ++*this;
340 }
341
343 {
344 Q_ASSERT_X(!m_atEnd, Q_FUNC_INFO, "operator* called on an iterator already at the end");
345 return m_currentMatch;
346 }
347
349 {
350 Q_ASSERT_X(!m_atEnd, Q_FUNC_INFO, "operator++ called on an iterator already at the end");
351 if (m_matchIterator.hasNext()) {
352 m_currentMatch = m_matchIterator.next();
353 } else {
354 m_currentMatch = QRegularExpressionMatch();
355 m_atEnd = true;
356 }
357
358 return *this;
359 }
360
367
368private:
369 // [input.iterators] imposes operator== on us. Unfortunately, it's not
370 // trivial to implement, so just do the bare minimum to satifisfy
371 // Cpp17EqualityComparable.
374 noexcept
375 {
376 return (&lhs == &rhs);
377 }
379
380 // This is what we really use in a range-based for.
383 noexcept
384 {
385 Q_UNUSED(rhs);
386 return lhs.m_atEnd;
387 }
390
391 QRegularExpressionMatchIterator m_matchIterator;
392 QRegularExpressionMatch m_currentMatch;
393 bool m_atEnd;
394};
395
396} // namespace QtPrivate
397
398Q_DECLARE_SHARED(QRegularExpressionMatchIterator)
399
401
402#endif // QREGULAREXPRESSION_H
\inmodule QtCore
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
\inmodule QtCore \reentrant
QRegularExpressionMatchIterator(QRegularExpressionMatchIterator &&iterator)=default
void swap(QRegularExpressionMatchIterator &other) noexcept
Swaps the iterator other with this iterator object.
friend QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIteratorSentinel end(const QRegularExpressionMatchIterator &)
QRegularExpressionMatchIterator & operator=(QRegularExpressionMatchIterator &&iterator) noexcept
Move-assigns the iterator to this object, and returns a reference to the result.
\inmodule QtCore \reentrant
void swap(QRegularExpressionMatch &other) noexcept
Swaps the match result other with this match result.
QRegularExpressionMatch & operator=(QRegularExpressionMatch &&match) noexcept
Move-assigns the match result match to this object, and returns a reference to the result.
QRegularExpressionMatch(QRegularExpressionMatch &&match)=default
\inmodule QtCore \reentrant
QRegularExpression(const QRegularExpression &re) noexcept
Constructs a QRegularExpression object as a copy of re.
~QRegularExpression()
Destroys the QRegularExpression object.
bool isValid() const
Returns true if the regular expression is a valid regular expression (that is, it contains no syntax ...
MatchOption
\value NoMatchOption No match options are set.
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.
int captureCount() const
Returns the number of capturing groups inside the pattern string, or -1 if the regular expression is ...
QRegularExpression & operator=(const QRegularExpression &re) noexcept
Assigns the regular expression re to this object, and returns a reference to the copy.
qsizetype patternErrorOffset() const
Returns the offset, inside the pattern string, at which an error was found when checking the validity...
static QRegularExpression fromWildcard(QStringView pattern, Qt::CaseSensitivity cs=Qt::CaseInsensitive, WildcardConversionOptions options=DefaultWildcardConversion)
void setPattern(const QString &pattern)
Sets the pattern string of the regular expression to pattern.
QStringList namedCaptureGroups() const
PatternOption
The PatternOption enum defines modifiers to the way the pattern string should be interpreted,...
MatchType
The MatchType enum defines the type of the match that should be attempted against the subject string.
friend class QRegularExpressionMatchIterator
QRegularExpression(QRegularExpression &&re)=default
QRegularExpressionMatch matchView(QStringView subjectView, qsizetype offset=0, MatchType matchType=NormalMatch, MatchOptions matchOptions=NoMatchOption) const
QRegularExpressionMatchIterator globalMatchView(QStringView subjectView, qsizetype offset=0, MatchType matchType=NormalMatch, MatchOptions matchOptions=NoMatchOption) const
QString errorString() const
Returns a textual description of the error found when checking the validity of the regular expression...
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,...
static QString anchoredPattern(const QString &expression)
static QString wildcardToRegularExpression(const QString &str, WildcardConversionOptions options=DefaultWildcardConversion)
QRegularExpression()
Constructs a QRegularExpression object with an empty pattern and no pattern options.
QSharedData & operator=(const QSharedData &)=delete
\inmodule QtCore
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
friend bool comparesEqual(const QRegularExpressionMatchIteratorRangeBasedForIterator &lhs, const QRegularExpressionMatchIteratorRangeBasedForIterator &rhs) noexcept
QRegularExpressionMatchIteratorRangeBasedForIterator operator++(int)
QRegularExpressionMatchIteratorRangeBasedForIterator(const QRegularExpressionMatchIterator &iterator)
QRegularExpressionMatchIteratorRangeBasedForIterator & operator++()
QString str
[2]
short next
Definition keywords.cpp:445
Combined button and popup list for selecting options.
\macro QT_NO_KEYWORDS >
CaseSensitivity
@ CaseInsensitive
QAnyStringView qToAnyStringViewIgnoringNull(const QStringLike &s) noexcept
#define Q_DECLARE_EQUALITY_COMPARABLE(...)
#define Q_DECL_ENUMERATOR_DEPRECATED_X(x)
#define Q_FUNC_INFO
constexpr bool operator!=(const timespec &t1, const timespec &t2)
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
bool comparesEqual(const QDir &lhs, const QDir &rhs)
Definition qdir.cpp:1819
#define Q_DECLARE_FLAGS(Flags, Enum)
Definition qflags.h:174
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
Definition qflags.h:194
GLuint64 key
GLenum GLuint GLintptr offset
GLuint name
GLuint in
GLubyte * pattern
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1220
#define Q_ASSERT_X(cond, x, msg)
Definition qrandom.cpp:48
QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIterator begin(const QRegularExpressionMatchIterator &iterator)
Q_CORE_EXPORT size_t qHash(const QRegularExpression &key, size_t seed=0) noexcept
Q_CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QRegularExpression &re)
Q_CORE_EXPORT QDataStream & operator>>(QDataStream &in, QRegularExpression &re)
#define QT_DECLARE_QESDP_SPECIALIZATION_DTOR_WITH_EXPORT(Class, ExportMacro)
QStringView qToStringViewIgnoringNull(const QStringLike &s) noexcept
static bool hasNext(const Symbols &symbols, int i)
Definition main.cpp:78
#define QT_REQUIRE_CONFIG(feature)
#define QT_DEPRECATED_VERSION_X_6_8(text)
#define Q_UNUSED(x)
static bool match(const uchar *found, uint foundLen, const char *target, uint targetLen)
ptrdiff_t qsizetype
Definition qtypes.h:165
static QString escape(const QString &input)
QTextStream out(stdout)
[7]
QSharedPointer< T > other(t)
[5]
this swap(other)
const QRegularExpression::MatchOptions matchOptions
const QRegularExpression::MatchType matchType
const QRegularExpression::MatchType matchType
const QRegularExpression::MatchOptions matchOptions
const QRegularExpression regularExpression