Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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// Qt-Security score:significant reason:trivial-impl-only
6
7#ifndef QREGULAREXPRESSION_H
8#define QREGULAREXPRESSION_H
9
10#include <QtCore/qglobal.h>
11#include <QtCore/qstring.h>
12#include <QtCore/qstringview.h>
13#include <QtCore/qshareddata.h>
14#include <QtCore/qvariant.h>
15
16#include <iterator>
17
19
20QT_BEGIN_NAMESPACE
21
22class QRegularExpressionMatch;
23class QRegularExpressionMatchIterator;
25class QRegularExpression;
26
27QT_DECLARE_QESDP_SPECIALIZATION_DTOR_WITH_EXPORT(QRegularExpressionPrivate, Q_CORE_EXPORT)
28
29Q_CORE_EXPORT size_t qHash(const QRegularExpression &key, size_t seed = 0) noexcept;
30
31class Q_CORE_EXPORT QRegularExpression
32{
33public:
34 enum PatternOption {
35 NoPatternOption = 0x0000,
36 CaseInsensitiveOption = 0x0001,
37 DotMatchesEverythingOption = 0x0002,
38 MultilineOption = 0x0004,
39 ExtendedPatternSyntaxOption = 0x0008,
40 InvertedGreedinessOption = 0x0010,
41 DontCaptureOption = 0x0020,
42 UseUnicodePropertiesOption = 0x0040,
43 // Formerly (no-ops deprecated in 5.12, removed 6.0):
44 // OptimizeOnFirstUsageOption = 0x0080,
45 // DontAutomaticallyOptimizeOption = 0x0100,
46 };
47 Q_DECLARE_FLAGS(PatternOptions, PatternOption)
48
49 PatternOptions patternOptions() const;
50 void setPatternOptions(PatternOptions options);
51
52 QRegularExpression();
53 explicit QRegularExpression(const QString &pattern, PatternOptions options = NoPatternOption);
54 QRegularExpression(const QRegularExpression &re) noexcept;
55 QRegularExpression(QRegularExpression &&re) = default;
56 ~QRegularExpression();
57 QRegularExpression &operator=(const QRegularExpression &re) noexcept;
58 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QRegularExpression)
59
60 void swap(QRegularExpression &other) noexcept { d.swap(other.d); }
61
62 QString pattern() const;
63 void setPattern(const QString &pattern);
64
65 [[nodiscard]]
66 bool isValid() const;
67 qsizetype patternErrorOffset() const;
68 QString errorString() const;
69
70 int captureCount() const;
71 QStringList namedCaptureGroups() const;
72
73 enum MatchType {
74 NormalMatch = 0,
75 PartialPreferCompleteMatch,
76 PartialPreferFirstMatch,
77 NoMatch
78 };
79
80 enum MatchOption {
81 NoMatchOption = 0x0000,
82 AnchorAtOffsetMatchOption = 0x0001,
83 AnchoredMatchOption Q_DECL_ENUMERATOR_DEPRECATED_X(
84 "Use AnchorAtOffsetMatchOption instead") = AnchorAtOffsetMatchOption, // Rename@Qt6.0
85 DontCheckSubjectStringMatchOption = 0x0002
86 };
87 Q_DECLARE_FLAGS(MatchOptions, MatchOption)
88
89 [[nodiscard]]
90 QRegularExpressionMatch match(const QString &subject,
91 qsizetype offset = 0,
92 MatchType matchType = NormalMatch,
93 MatchOptions matchOptions = NoMatchOption) const;
94
95#if QT_DEPRECATED_SINCE(6, 8)
96 [[nodiscard]]
97 QT_DEPRECATED_VERSION_X_6_8("Use matchView instead.")
98 QRegularExpressionMatch match(QStringView subjectView,
99 qsizetype offset = 0,
100 MatchType matchType = NormalMatch,
101 MatchOptions matchOptions = NoMatchOption) const;
102#endif
103
104 [[nodiscard]]
105 QRegularExpressionMatch matchView(QStringView subjectView,
106 qsizetype offset = 0,
107 MatchType matchType = NormalMatch,
108 MatchOptions matchOptions = NoMatchOption) const;
109
110 [[nodiscard]]
111 QRegularExpressionMatchIterator globalMatch(const QString &subject,
112 qsizetype offset = 0,
113 MatchType matchType = NormalMatch,
114 MatchOptions matchOptions = NoMatchOption) const;
115
116#if QT_DEPRECATED_SINCE(6, 8)
117 [[nodiscard]]
118 QT_DEPRECATED_VERSION_X_6_8("Use globalMatchView instead.")
119 QRegularExpressionMatchIterator globalMatch(QStringView subjectView,
120 qsizetype offset = 0,
121 MatchType matchType = NormalMatch,
122 MatchOptions matchOptions = NoMatchOption) const;
123#endif
124
125 [[nodiscard]]
126 QRegularExpressionMatchIterator globalMatchView(QStringView subjectView,
127 qsizetype offset = 0,
128 MatchType matchType = NormalMatch,
129 MatchOptions matchOptions = NoMatchOption) const;
130
131 void optimize() const;
132
133 enum WildcardConversionOption {
134 DefaultWildcardConversion = 0x0,
135 UnanchoredWildcardConversion = 0x1,
136 NonPathWildcardConversion = 0x2,
137 };
138 Q_DECLARE_FLAGS(WildcardConversionOptions, WildcardConversionOption)
139
140 static QString escape(const QString &str)
141 {
142 return escape(qToStringViewIgnoringNull(str));
143 }
144
145 static QString wildcardToRegularExpression(const QString &str, WildcardConversionOptions options = DefaultWildcardConversion)
146 {
147 return wildcardToRegularExpression(qToStringViewIgnoringNull(str), options);
148 }
149
150 static inline QString anchoredPattern(const QString &expression)
151 {
152 return anchoredPattern(qToStringViewIgnoringNull(expression));
153 }
154
155 static QString escape(QStringView str);
156 static QString wildcardToRegularExpression(QStringView str, WildcardConversionOptions options = DefaultWildcardConversion);
157 static QString anchoredPattern(QStringView expression);
158
159 static QRegularExpression fromWildcard(QStringView pattern, Qt::CaseSensitivity cs = Qt::CaseInsensitive,
160 WildcardConversionOptions options = DefaultWildcardConversion);
161#if QT_CORE_REMOVED_SINCE(6, 8)
162 bool operator==(const QRegularExpression &re) const;
163 inline bool operator!=(const QRegularExpression &re) const { return !operator==(re); }
164#endif
165private:
166 friend Q_CORE_EXPORT bool comparesEqual(const QRegularExpression &lhs,
167 const QRegularExpression &rhs) noexcept;
168 Q_DECLARE_EQUALITY_COMPARABLE(QRegularExpression)
169
170 friend struct QRegularExpressionPrivate;
171 friend class QRegularExpressionMatch;
172 friend struct QRegularExpressionMatchPrivate;
173 friend class QRegularExpressionMatchIterator;
174 friend Q_CORE_EXPORT size_t qHash(const QRegularExpression &key, size_t seed) noexcept;
175
176 QRegularExpression(QRegularExpressionPrivate &dd);
177 QExplicitlySharedDataPointer<QRegularExpressionPrivate> d;
178};
179
181Q_DECLARE_OPERATORS_FOR_FLAGS(QRegularExpression::PatternOptions)
182Q_DECLARE_OPERATORS_FOR_FLAGS(QRegularExpression::MatchOptions)
183Q_DECLARE_OPERATORS_FOR_FLAGS(QRegularExpression::WildcardConversionOptions)
184
185#ifndef QT_NO_DATASTREAM
186Q_CORE_EXPORT QDataStream &operator<<(QDataStream &out, const QRegularExpression &re);
187Q_CORE_EXPORT QDataStream &operator>>(QDataStream &in, QRegularExpression &re);
188#endif
189
190#ifndef QT_NO_DEBUG_STREAM
191Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QRegularExpression &re);
192Q_CORE_EXPORT QDebug operator<<(QDebug debug, QRegularExpression::PatternOptions patternOptions);
193#endif
194
195[[nodiscard]] inline qsizetype QStringView::count(const QRegularExpression &re) const
196{
197 return QtPrivate::count(*this, re);
198}
199
201QT_DECLARE_QESDP_SPECIALIZATION_DTOR_WITH_EXPORT(QRegularExpressionMatchPrivate, Q_CORE_EXPORT)
202
203class Q_CORE_EXPORT QRegularExpressionMatch
204{
205public:
206 QRegularExpressionMatch();
207 ~QRegularExpressionMatch();
208 QRegularExpressionMatch(const QRegularExpressionMatch &match);
209 QRegularExpressionMatch(QRegularExpressionMatch &&match) = default;
210 QRegularExpressionMatch &operator=(const QRegularExpressionMatch &match);
211 QRegularExpressionMatch &operator=(QRegularExpressionMatch &&match) noexcept
212 { d.swap(match.d); return *this; }
213 void swap(QRegularExpressionMatch &other) noexcept { d.swap(other.d); }
214
215 QRegularExpression regularExpression() const;
216 QRegularExpression::MatchType matchType() const;
217 QRegularExpression::MatchOptions matchOptions() const;
218
219 bool hasMatch() const;
220 bool hasPartialMatch() const;
221
222 bool isValid() const;
223
224 int lastCapturedIndex() const;
225
226#if QT_CORE_REMOVED_SINCE(6, 8)
227 bool hasCaptured(const QString &name) const
228 { return hasCaptured(qToAnyStringViewIgnoringNull(name)); }
229 bool hasCaptured(QStringView name) const;
230#endif
231 bool hasCaptured(QAnyStringView name) const;
232 bool hasCaptured(int nth) const;
233
234 QString captured(int nth = 0) const;
235 QStringView capturedView(int nth = 0) const;
236
237#if QT_CORE_REMOVED_SINCE(6, 8)
238 QString captured(const QString &name) const
239 { return captured(qToAnyStringViewIgnoringNull(name)); }
240
241 QString captured(QStringView name) const;
242 QStringView capturedView(QStringView name) const;
243#endif
244 QString captured(QAnyStringView name) const;
245 QStringView capturedView(QAnyStringView name) const;
246
247 QStringList capturedTexts() const;
248
249 qsizetype capturedStart(int nth = 0) const;
250 qsizetype capturedLength(int nth = 0) const;
251 qsizetype capturedEnd(int nth = 0) const;
252
253#if QT_CORE_REMOVED_SINCE(6, 8)
254 qsizetype capturedStart(const QString &name) const
255 { return capturedStart(qToAnyStringViewIgnoringNull(name)); }
256 qsizetype capturedLength(const QString &name) const
257 { return capturedLength(qToAnyStringViewIgnoringNull(name)); }
258 qsizetype capturedEnd(const QString &name) const
259 { return capturedEnd(qToAnyStringViewIgnoringNull(name)); }
260
261 qsizetype capturedStart(QStringView name) const;
262 qsizetype capturedLength(QStringView name) const;
263 qsizetype capturedEnd(QStringView name) const;
264#endif
265 qsizetype capturedStart(QAnyStringView name) const;
266 qsizetype capturedLength(QAnyStringView name) const;
267 qsizetype capturedEnd(QAnyStringView name) const;
268
269private:
270 friend class QRegularExpression;
271 friend struct QRegularExpressionMatchPrivate;
272 friend class QRegularExpressionMatchIterator;
273
274 QRegularExpressionMatch(QRegularExpressionMatchPrivate &dd);
275 QExplicitlySharedDataPointer<QRegularExpressionMatchPrivate> d;
276};
277
279
280#ifndef QT_NO_DEBUG_STREAM
281Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QRegularExpressionMatch &match);
282#endif
283
284namespace QtPrivate {
287}
288
290QT_DECLARE_QESDP_SPECIALIZATION_DTOR_WITH_EXPORT(QRegularExpressionMatchIteratorPrivate, Q_CORE_EXPORT)
291
293{
294public:
295 QRegularExpressionMatchIterator();
296 ~QRegularExpressionMatchIterator();
297 QRegularExpressionMatchIterator(const QRegularExpressionMatchIterator &iterator);
298 QRegularExpressionMatchIterator(QRegularExpressionMatchIterator &&iterator) = default;
299 QRegularExpressionMatchIterator &operator=(const QRegularExpressionMatchIterator &iterator);
300 QRegularExpressionMatchIterator &operator=(QRegularExpressionMatchIterator &&iterator) noexcept
301 { d.swap(iterator.d); return *this; }
302 void swap(QRegularExpressionMatchIterator &other) noexcept { d.swap(other.d); }
303
304 bool isValid() const;
305
306 bool hasNext() const;
307 QRegularExpressionMatch next();
308 QRegularExpressionMatch peekNext() const;
309
310 QRegularExpression regularExpression() const;
311 QRegularExpression::MatchType matchType() const;
312 QRegularExpression::MatchOptions matchOptions() const;
313
314private:
315 friend class QRegularExpression;
316 friend Q_CORE_EXPORT QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIterator begin(const QRegularExpressionMatchIterator &iterator);
317 friend QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIteratorSentinel end(const QRegularExpressionMatchIterator &) { return {}; }
318
319 QRegularExpressionMatchIterator(QRegularExpressionMatchIteratorPrivate &dd);
320 QExplicitlySharedDataPointer<QRegularExpressionMatchIteratorPrivate> d;
321};
322
323namespace QtPrivate {
324
325// support for range-based for loop
327{
328public:
329 using value_type = QRegularExpressionMatch;
330 using difference_type = int;
331 using reference_type = const QRegularExpressionMatch &;
332 using pointer_type = const QRegularExpressionMatch *;
333 using iterator_category = std::forward_iterator_tag;
334
339
340 explicit QRegularExpressionMatchIteratorRangeBasedForIterator(const QRegularExpressionMatchIterator &iterator)
343 m_atEnd(false)
344 {
345 ++*this;
346 }
347
348 const QRegularExpressionMatch &operator*() const
349 {
350 Q_ASSERT_X(!m_atEnd, Q_FUNC_INFO, "operator* called on an iterator already at the end");
351 return m_currentMatch;
352 }
353
355 {
356 Q_ASSERT_X(!m_atEnd, Q_FUNC_INFO, "operator++ called on an iterator already at the end");
357 if (m_matchIterator.hasNext()) {
358 m_currentMatch = m_matchIterator.next();
359 } else {
360 m_currentMatch = QRegularExpressionMatch();
361 m_atEnd = true;
362 }
363
364 return *this;
365 }
366
373
374private:
375 // [input.iterators] imposes operator== on us. Unfortunately, it's not
376 // trivial to implement, so just do the bare minimum to satifisfy
377 // Cpp17EqualityComparable.
380 noexcept
381 {
382 return (&lhs == &rhs);
383 }
385
386 // This is what we really use in a range-based for.
396
398 QRegularExpressionMatch m_currentMatch;
399 bool m_atEnd;
400};
401
402} // namespace QtPrivate
403
405
406QT_END_NAMESPACE
407
408#endif // QREGULAREXPRESSION_H
\inmodule QtCore\reentrant
Definition qdatastream.h:50
\inmodule QtCore \reentrant
\inmodule QtCore \reentrant
QDebug operator<<(QDebug debug, const QRegularExpressionMatch &match)
Writes the match object match into the debug object debug for debugging purposes.
\inmodule QtCore \reentrant
QDataStream & operator<<(QDataStream &out, const QRegularExpression &re)
Writes the regular expression re to stream out.
QDataStream & operator>>(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.
friend bool comparesEqual(const QRegularExpressionMatchIteratorRangeBasedForIterator &lhs, const QRegularExpressionMatchIteratorRangeBasedForIterator &rhs) noexcept
QRegularExpressionMatchIteratorRangeBasedForIterator operator++(int)
QRegularExpressionMatchIteratorRangeBasedForIterator(const QRegularExpressionMatchIterator &iterator)
QRegularExpressionMatchIteratorRangeBasedForIterator & operator++()
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2582
QDebug operator<<(QDebug debug, QIODevice::OpenMode modes)
static pcre2_jit_stack_16 * qtPcreCallback(void *)
static int convertToPcreOptions(QRegularExpression::PatternOptions patternOptions)
bool comparesEqual(const QRegularExpression &lhs, const QRegularExpression &rhs) noexcept
Q_DECL_COLD_FUNCTION void qtWarnAboutInvalidRegularExpression(const QString &pattern, const char *cls, const char *method)
static bool isJitEnabled()
static int safe_pcre2_match_16(const pcre2_code_16 *code, PCRE2_SPTR16 subject, qsizetype length, qsizetype startOffset, int options, pcre2_match_data_16 *matchData, pcre2_match_context_16 *matchContext)
QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIterator begin(const QRegularExpressionMatchIterator &iterator)
QT_REQUIRE_CONFIG(regularexpression)
QRegularExpressionMatchIteratorPrivate(const QRegularExpression &re, QRegularExpression::MatchType matchType, QRegularExpression::MatchOptions matchOptions, const QRegularExpressionMatch &next)
const QRegularExpression::MatchOptions matchOptions
const QRegularExpression::MatchType matchType
QRegularExpressionMatchPrivate(const QRegularExpression &re, const QString &subjectStorage, QStringView subject, QRegularExpression::MatchType matchType, QRegularExpression::MatchOptions matchOptions)
const QRegularExpression::MatchType matchType
const QRegularExpression::MatchOptions matchOptions
QRegularExpressionMatch nextMatch() const
const QRegularExpression regularExpression
QRegularExpressionPrivate(const QRegularExpressionPrivate &other)
int captureIndexForName(QAnyStringView name) const
void doMatch(QRegularExpressionMatchPrivate *priv, qsizetype offset, CheckSubjectStringOption checkSubjectStringOption=CheckSubjectString, const QRegularExpressionMatchPrivate *previous=nullptr) const
QRegularExpression::PatternOptions patternOptions