1/****************************************************************************
2**
3** Copyright (C) 2012 Giuseppe D'Angelo <dangelog@gmail.com>.
4** Copyright (C) 2012 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#ifndef QREGULAREXPRESSION_H
42#define QREGULAREXPRESSION_H
43
44#include <QtCore/qglobal.h>
45
46#include <QtCore/qstring.h>
47#include <QtCore/qstringlist.h>
48#include <QtCore/qshareddata.h>
49#include <QtCore/qvariant.h>
50
51QT_REQUIRE_CONFIG(regularexpression);
52
53QT_BEGIN_NAMESPACE
54
55class QStringView;
56
57class QRegularExpressionMatch;
58class QRegularExpressionMatchIterator;
59struct QRegularExpressionPrivate;
60class QRegularExpression;
61
62Q_CORE_EXPORT uint qHash(const QRegularExpression &key, uint seed = 0) Q_DECL_NOTHROW;
63
64class Q_CORE_EXPORT QRegularExpression
65{
66public:
67 enum PatternOption {
68 NoPatternOption = 0x0000,
69 CaseInsensitiveOption = 0x0001,
70 DotMatchesEverythingOption = 0x0002,
71 MultilineOption = 0x0004,
72 ExtendedPatternSyntaxOption = 0x0008,
73 InvertedGreedinessOption = 0x0010,
74 DontCaptureOption = 0x0020,
75 UseUnicodePropertiesOption = 0x0040,
76 OptimizeOnFirstUsageOption Q_DECL_ENUMERATOR_DEPRECATED_X("This option does not have any effect since Qt 5.12") = 0x0080,
77 DontAutomaticallyOptimizeOption Q_DECL_ENUMERATOR_DEPRECATED_X("This option does not have any effect since Qt 5.12") = 0x0100,
78 };
79 Q_DECLARE_FLAGS(PatternOptions, PatternOption)
80
81 PatternOptions patternOptions() const;
82 void setPatternOptions(PatternOptions options);
83
84 QRegularExpression();
85 explicit QRegularExpression(const QString &pattern, PatternOptions options = NoPatternOption);
86 QRegularExpression(const QRegularExpression &re);
87 ~QRegularExpression();
88 QRegularExpression &operator=(const QRegularExpression &re);
89
90#ifdef Q_COMPILER_RVALUE_REFS
91 QRegularExpression &operator=(QRegularExpression &&re) Q_DECL_NOTHROW
92 { d.swap(re.d); return *this; }
93#endif
94
95 void swap(QRegularExpression &other) Q_DECL_NOTHROW { d.swap(other.d); }
96
97 QString pattern() const;
98 void setPattern(const QString &pattern);
99
100 bool isValid() const;
101 int patternErrorOffset() const;
102 QString errorString() const;
103
104 int captureCount() const;
105 QStringList namedCaptureGroups() const;
106
107 enum MatchType {
108 NormalMatch = 0,
109 PartialPreferCompleteMatch,
110 PartialPreferFirstMatch,
111 NoMatch
112 };
113
114 enum MatchOption {
115 NoMatchOption = 0x0000,
116 AnchoredMatchOption = 0x0001,
117 DontCheckSubjectStringMatchOption = 0x0002
118 };
119 Q_DECLARE_FLAGS(MatchOptions, MatchOption)
120
121 QRegularExpressionMatch match(const QString &subject,
122 int offset = 0,
123 MatchType matchType = NormalMatch,
124 MatchOptions matchOptions = NoMatchOption) const;
125
126 QRegularExpressionMatch match(const QStringRef &subjectRef,
127 int offset = 0,
128 MatchType matchType = NormalMatch,
129 MatchOptions matchOptions = NoMatchOption) const;
130
131 QRegularExpressionMatchIterator globalMatch(const QString &subject,
132 int offset = 0,
133 MatchType matchType = NormalMatch,
134 MatchOptions matchOptions = NoMatchOption) const;
135
136 QRegularExpressionMatchIterator globalMatch(const QStringRef &subjectRef,
137 int offset = 0,
138 MatchType matchType = NormalMatch,
139 MatchOptions matchOptions = NoMatchOption) const;
140
141 void optimize() const;
142
143 static QString escape(const QString &str);
144 static QString wildcardToRegularExpression(const QString &str);
145 static inline QString anchoredPattern(const QString &expression)
146 {
147 return QLatin1String("\\A(?:")
148 + expression
149 + QLatin1String(")\\z");
150 }
151
152 bool operator==(const QRegularExpression &re) const;
153 inline bool operator!=(const QRegularExpression &re) const { return !operator==(re); }
154
155private:
156 friend struct QRegularExpressionPrivate;
157 friend class QRegularExpressionMatch;
158 friend struct QRegularExpressionMatchPrivate;
159 friend class QRegularExpressionMatchIterator;
160 friend Q_CORE_EXPORT uint qHash(const QRegularExpression &key, uint seed) Q_DECL_NOTHROW;
161
162 QRegularExpression(QRegularExpressionPrivate &dd);
163 QExplicitlySharedDataPointer<QRegularExpressionPrivate> d;
164};
165
166Q_DECLARE_SHARED(QRegularExpression)
167Q_DECLARE_OPERATORS_FOR_FLAGS(QRegularExpression::PatternOptions)
168Q_DECLARE_OPERATORS_FOR_FLAGS(QRegularExpression::MatchOptions)
169
170#ifndef QT_NO_DATASTREAM
171Q_CORE_EXPORT QDataStream &operator<<(QDataStream &out, const QRegularExpression &re);
172Q_CORE_EXPORT QDataStream &operator>>(QDataStream &in, QRegularExpression &re);
173#endif
174
175#ifndef QT_NO_DEBUG_STREAM
176Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QRegularExpression &re);
177Q_CORE_EXPORT QDebug operator<<(QDebug debug, QRegularExpression::PatternOptions patternOptions);
178#endif
179
180struct QRegularExpressionMatchPrivate;
181
182class Q_CORE_EXPORT QRegularExpressionMatch
183{
184public:
185 QRegularExpressionMatch();
186 ~QRegularExpressionMatch();
187 QRegularExpressionMatch(const QRegularExpressionMatch &match);
188 QRegularExpressionMatch &operator=(const QRegularExpressionMatch &match);
189
190#ifdef Q_COMPILER_RVALUE_REFS
191 QRegularExpressionMatch &operator=(QRegularExpressionMatch &&match) Q_DECL_NOTHROW
192 { d.swap(match.d); return *this; }
193#endif
194 void swap(QRegularExpressionMatch &other) Q_DECL_NOTHROW { d.swap(other.d); }
195
196 QRegularExpression regularExpression() const;
197 QRegularExpression::MatchType matchType() const;
198 QRegularExpression::MatchOptions matchOptions() const;
199
200 bool hasMatch() const;
201 bool hasPartialMatch() const;
202
203 bool isValid() const;
204
205 int lastCapturedIndex() const;
206
207 QString captured(int nth = 0) const;
208 QStringRef capturedRef(int nth = 0) const;
209 QStringView capturedView(int nth = 0) const;
210
211#if QT_STRINGVIEW_LEVEL < 2
212 QString captured(const QString &name) const;
213 QStringRef capturedRef(const QString &name) const;
214#endif
215
216 QString captured(QStringView name) const;
217 QStringRef capturedRef(QStringView name) const;
218 QStringView capturedView(QStringView name) const;
219
220 QStringList capturedTexts() const;
221
222 int capturedStart(int nth = 0) const;
223 int capturedLength(int nth = 0) const;
224 int capturedEnd(int nth = 0) const;
225
226#if QT_STRINGVIEW_LEVEL < 2
227 int capturedStart(const QString &name) const;
228 int capturedLength(const QString &name) const;
229 int capturedEnd(const QString &name) const;
230#endif
231
232 int capturedStart(QStringView name) const;
233 int capturedLength(QStringView name) const;
234 int capturedEnd(QStringView name) const;
235
236private:
237 friend class QRegularExpression;
238 friend struct QRegularExpressionMatchPrivate;
239 friend class QRegularExpressionMatchIterator;
240
241 QRegularExpressionMatch(QRegularExpressionMatchPrivate &dd);
242 QSharedDataPointer<QRegularExpressionMatchPrivate> d;
243};
244
245Q_DECLARE_SHARED(QRegularExpressionMatch)
246
247#ifndef QT_NO_DEBUG_STREAM
248Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QRegularExpressionMatch &match);
249#endif
250
251struct QRegularExpressionMatchIteratorPrivate;
252
253class Q_CORE_EXPORT QRegularExpressionMatchIterator
254{
255public:
256 QRegularExpressionMatchIterator();
257 ~QRegularExpressionMatchIterator();
258 QRegularExpressionMatchIterator(const QRegularExpressionMatchIterator &iterator);
259 QRegularExpressionMatchIterator &operator=(const QRegularExpressionMatchIterator &iterator);
260#ifdef Q_COMPILER_RVALUE_REFS
261 QRegularExpressionMatchIterator &operator=(QRegularExpressionMatchIterator &&iterator) Q_DECL_NOTHROW
262 { d.swap(iterator.d); return *this; }
263#endif
264 void swap(QRegularExpressionMatchIterator &other) Q_DECL_NOTHROW { d.swap(other.d); }
265
266 bool isValid() const;
267
268 bool hasNext() const;
269 QRegularExpressionMatch next();
270 QRegularExpressionMatch peekNext() const;
271
272 QRegularExpression regularExpression() const;
273 QRegularExpression::MatchType matchType() const;
274 QRegularExpression::MatchOptions matchOptions() const;
275
276private:
277 friend class QRegularExpression;
278
279 QRegularExpressionMatchIterator(QRegularExpressionMatchIteratorPrivate &dd);
280 QSharedDataPointer<QRegularExpressionMatchIteratorPrivate> d;
281};
282
283Q_DECLARE_SHARED(QRegularExpressionMatchIterator)
284
285QT_END_NAMESPACE
286
287#endif // QREGULAREXPRESSION_H
288