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
196QT_DECLARE_QESDP_SPECIALIZATION_DTOR_WITH_EXPORT(QRegularExpressionMatchPrivate, Q_CORE_EXPORT)
197
198class Q_CORE_EXPORT QRegularExpressionMatch
199{
200public:
201 QRegularExpressionMatch();
202 ~QRegularExpressionMatch();
203 QRegularExpressionMatch(const QRegularExpressionMatch &match);
204 QRegularExpressionMatch(QRegularExpressionMatch &&match) = default;
205 QRegularExpressionMatch &operator=(const QRegularExpressionMatch &match);
206 QRegularExpressionMatch &operator=(QRegularExpressionMatch &&match) noexcept
207 { d.swap(match.d); return *this; }
208 void swap(QRegularExpressionMatch &other) noexcept { d.swap(other.d); }
209
210 QRegularExpression regularExpression() const;
211 QRegularExpression::MatchType matchType() const;
212 QRegularExpression::MatchOptions matchOptions() const;
213
214 bool hasMatch() const;
215 bool hasPartialMatch() const;
216
217 bool isValid() const;
218
219 int lastCapturedIndex() const;
220
221#if QT_CORE_REMOVED_SINCE(6, 8)
222 bool hasCaptured(const QString &name) const
223 { return hasCaptured(qToAnyStringViewIgnoringNull(name)); }
224 bool hasCaptured(QStringView name) const;
225#endif
226 bool hasCaptured(QAnyStringView name) const;
227 bool hasCaptured(int nth) const;
228
229 QString captured(int nth = 0) const;
230 QStringView capturedView(int nth = 0) const;
231
232#if QT_CORE_REMOVED_SINCE(6, 8)
233 QString captured(const QString &name) const
234 { return captured(qToAnyStringViewIgnoringNull(name)); }
235
236 QString captured(QStringView name) const;
237 QStringView capturedView(QStringView name) const;
238#endif
239 QString captured(QAnyStringView name) const;
240 QStringView capturedView(QAnyStringView name) const;
241
242 QStringList capturedTexts() const;
243
244 qsizetype capturedStart(int nth = 0) const;
245 qsizetype capturedLength(int nth = 0) const;
246 qsizetype capturedEnd(int nth = 0) const;
247
248#if QT_CORE_REMOVED_SINCE(6, 8)
249 qsizetype capturedStart(const QString &name) const
250 { return capturedStart(qToAnyStringViewIgnoringNull(name)); }
251 qsizetype capturedLength(const QString &name) const
252 { return capturedLength(qToAnyStringViewIgnoringNull(name)); }
253 qsizetype capturedEnd(const QString &name) const
254 { return capturedEnd(qToAnyStringViewIgnoringNull(name)); }
255
256 qsizetype capturedStart(QStringView name) const;
257 qsizetype capturedLength(QStringView name) const;
258 qsizetype capturedEnd(QStringView name) const;
259#endif
260 qsizetype capturedStart(QAnyStringView name) const;
261 qsizetype capturedLength(QAnyStringView name) const;
262 qsizetype capturedEnd(QAnyStringView name) const;
263
264private:
265 friend class QRegularExpression;
266 friend struct QRegularExpressionMatchPrivate;
267 friend class QRegularExpressionMatchIterator;
268
269 QRegularExpressionMatch(QRegularExpressionMatchPrivate &dd);
270 QExplicitlySharedDataPointer<QRegularExpressionMatchPrivate> d;
271};
272
274
275#ifndef QT_NO_DEBUG_STREAM
276Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QRegularExpressionMatch &match);
277#endif
278
279namespace QtPrivate {
282}
283
285QT_DECLARE_QESDP_SPECIALIZATION_DTOR_WITH_EXPORT(QRegularExpressionMatchIteratorPrivate, Q_CORE_EXPORT)
286
288{
289public:
290 QRegularExpressionMatchIterator();
291 ~QRegularExpressionMatchIterator();
292 QRegularExpressionMatchIterator(const QRegularExpressionMatchIterator &iterator);
293 QRegularExpressionMatchIterator(QRegularExpressionMatchIterator &&iterator) = default;
294 QRegularExpressionMatchIterator &operator=(const QRegularExpressionMatchIterator &iterator);
295 QRegularExpressionMatchIterator &operator=(QRegularExpressionMatchIterator &&iterator) noexcept
296 { d.swap(iterator.d); return *this; }
297 void swap(QRegularExpressionMatchIterator &other) noexcept { d.swap(other.d); }
298
299 bool isValid() const;
300
301 bool hasNext() const;
302 QRegularExpressionMatch next();
303 QRegularExpressionMatch peekNext() const;
304
305 QRegularExpression regularExpression() const;
306 QRegularExpression::MatchType matchType() const;
307 QRegularExpression::MatchOptions matchOptions() const;
308
309private:
310 friend class QRegularExpression;
311 friend Q_CORE_EXPORT QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIterator begin(const QRegularExpressionMatchIterator &iterator);
312 friend QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIteratorSentinel end(const QRegularExpressionMatchIterator &) { return {}; }
313
314 QRegularExpressionMatchIterator(QRegularExpressionMatchIteratorPrivate &dd);
315 QExplicitlySharedDataPointer<QRegularExpressionMatchIteratorPrivate> d;
316};
317
318namespace QtPrivate {
319
320// support for range-based for loop
322{
323public:
324 using value_type = QRegularExpressionMatch;
325 using difference_type = int;
326 using reference_type = const QRegularExpressionMatch &;
327 using pointer_type = const QRegularExpressionMatch *;
328 using iterator_category = std::forward_iterator_tag;
329
334
335 explicit QRegularExpressionMatchIteratorRangeBasedForIterator(const QRegularExpressionMatchIterator &iterator)
338 m_atEnd(false)
339 {
340 ++*this;
341 }
342
343 const QRegularExpressionMatch &operator*() const
344 {
345 Q_ASSERT_X(!m_atEnd, Q_FUNC_INFO, "operator* called on an iterator already at the end");
346 return m_currentMatch;
347 }
348
350 {
351 Q_ASSERT_X(!m_atEnd, Q_FUNC_INFO, "operator++ called on an iterator already at the end");
352 if (m_matchIterator.hasNext()) {
353 m_currentMatch = m_matchIterator.next();
354 } else {
355 m_currentMatch = QRegularExpressionMatch();
356 m_atEnd = true;
357 }
358
359 return *this;
360 }
361
368
369private:
370 // [input.iterators] imposes operator== on us. Unfortunately, it's not
371 // trivial to implement, so just do the bare minimum to satifisfy
372 // Cpp17EqualityComparable.
375 noexcept
376 {
377 return (&lhs == &rhs);
378 }
380
381 // This is what we really use in a range-based for.
391
393 QRegularExpressionMatch m_currentMatch;
394 bool m_atEnd;
395};
396
397} // namespace QtPrivate
398
400
401QT_END_NAMESPACE
402
403#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:2568
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