1 /****************************************************************************
3 ** Copyright (C) 2012 Giuseppe D'Angelo <dangelog@gmail.com>.
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QREGULAREXPRESSION_H
43 #define QREGULAREXPRESSION_H
47 #include <QtCore/qstring.h>
48 #include <QtCore/qshareddata.h>
49 #include <QtCore/qvariant.h>
55 class QRegularExpressionMatch;
56 class QRegularExpressionMatchIterator;
57 struct QRegularExpressionPrivate;
59 class Q_CORE_EXPORT QRegularExpression
63 NoPatternOption = 0x0000,
64 CaseInsensitiveOption = 0x0001,
65 DotMatchesEverythingOption = 0x0002,
66 MultilineOption = 0x0004,
67 ExtendedPatternSyntaxOption = 0x0008,
68 InvertedGreedinessOption = 0x0010,
69 DontCaptureOption = 0x0020,
70 UseUnicodePropertiesOption = 0x0040
72 Q_DECLARE_FLAGS(PatternOptions, PatternOption)
74 PatternOptions patternOptions() const;
75 void setPatternOptions(PatternOptions options);
78 explicit QRegularExpression(const QString &pattern, PatternOptions options = NoPatternOption);
79 QRegularExpression(const QRegularExpression &re);
80 ~QRegularExpression();
81 QRegularExpression &operator=(const QRegularExpression &re);
83 #ifdef Q_COMPILER_RVALUE_REFS
84 inline QRegularExpression &operator=(QRegularExpression &&re)
85 { d.swap(re.d); return *this; }
88 inline void swap(QRegularExpression &re) { d.swap(re.d); }
90 QString pattern() const;
91 void setPattern(const QString &pattern);
94 int patternErrorOffset() const;
95 QString errorString() const;
97 int captureCount() const;
101 PartialPreferCompleteMatch,
102 PartialPreferFirstMatch
106 NoMatchOption = 0x0000,
107 AnchoredMatchOption = 0x0001
109 Q_DECLARE_FLAGS(MatchOptions, MatchOption)
111 QRegularExpressionMatch match(const QString &subject,
113 MatchType matchType = NormalMatch,
114 MatchOptions matchOptions = NoMatchOption) const;
116 QRegularExpressionMatchIterator globalMatch(const QString &subject,
118 MatchType matchType = NormalMatch,
119 MatchOptions matchOptions = NoMatchOption) const;
121 static QString escape(const QString &str);
123 bool operator==(const QRegularExpression &re) const;
124 inline bool operator!=(const QRegularExpression &re) const { return !operator==(re); }
127 friend struct QRegularExpressionPrivate;
128 friend class QRegularExpressionMatch;
129 friend struct QRegularExpressionMatchPrivate;
130 friend class QRegularExpressionMatchIterator;
132 QRegularExpression(QRegularExpressionPrivate &dd);
133 QExplicitlySharedDataPointer<QRegularExpressionPrivate> d;
136 Q_DECLARE_OPERATORS_FOR_FLAGS(QRegularExpression::PatternOptions)
137 Q_DECLARE_OPERATORS_FOR_FLAGS(QRegularExpression::MatchOptions)
138 Q_DECLARE_TYPEINFO(QRegularExpression, Q_MOVABLE_TYPE);
140 #ifndef QT_NO_DATASTREAM
141 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &out, const QRegularExpression &re);
142 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &in, QRegularExpression &re);
145 #ifndef QT_NO_DEBUG_STREAM
146 Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QRegularExpression &re);
147 Q_CORE_EXPORT QDebug operator<<(QDebug debug, QRegularExpression::PatternOptions patternOptions);
150 struct QRegularExpressionMatchPrivate;
152 class Q_CORE_EXPORT QRegularExpressionMatch
155 ~QRegularExpressionMatch();
156 QRegularExpressionMatch(const QRegularExpressionMatch &match);
157 QRegularExpressionMatch &operator=(const QRegularExpressionMatch &match);
159 #ifdef Q_COMPILER_RVALUE_REFS
160 inline QRegularExpressionMatch &operator=(QRegularExpressionMatch &&match)
161 { d.swap(match.d); return *this; }
163 inline void swap(QRegularExpressionMatch &match) { d.swap(match.d); }
165 QRegularExpression regularExpression() const;
166 QRegularExpression::MatchType matchType() const;
167 QRegularExpression::MatchOptions matchOptions() const;
169 bool hasMatch() const;
170 bool hasPartialMatch() const;
172 bool isValid() const;
174 int lastCapturedIndex() const;
176 QString captured(int nth = 0) const;
177 QStringRef capturedRef(int nth = 0) const;
179 QString captured(const QString &name) const;
180 QStringRef capturedRef(const QString &name) const;
182 QStringList capturedTexts() const;
184 int capturedStart(int nth = 0) const;
185 int capturedLength(int nth = 0) const;
186 int capturedEnd(int nth = 0) const;
188 int capturedStart(const QString &name) const;
189 int capturedLength(const QString &name) const;
190 int capturedEnd(const QString &name) const;
193 friend class QRegularExpression;
194 friend struct QRegularExpressionMatchPrivate;
195 friend class QRegularExpressionMatchIterator;
197 QRegularExpressionMatch(QRegularExpressionMatchPrivate &dd);
198 QSharedDataPointer<QRegularExpressionMatchPrivate> d;
201 Q_DECLARE_TYPEINFO(QRegularExpressionMatch, Q_MOVABLE_TYPE);
203 #ifndef QT_NO_DEBUG_STREAM
204 Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QRegularExpressionMatch &match);
207 struct QRegularExpressionMatchIteratorPrivate;
209 class Q_CORE_EXPORT QRegularExpressionMatchIterator
212 ~QRegularExpressionMatchIterator();
213 QRegularExpressionMatchIterator(const QRegularExpressionMatchIterator &iterator);
214 QRegularExpressionMatchIterator &operator=(const QRegularExpressionMatchIterator &iterator);
215 #ifdef Q_COMPILER_RVALUE_REFS
216 inline QRegularExpressionMatchIterator &operator=(QRegularExpressionMatchIterator &&iterator)
217 { d.swap(iterator.d); return *this; }
219 void swap(QRegularExpressionMatchIterator &iterator) { d.swap(iterator.d); }
221 bool isValid() const;
223 bool hasNext() const;
224 QRegularExpressionMatch next();
225 QRegularExpressionMatch peekNext() const;
227 QRegularExpression regularExpression() const;
228 QRegularExpression::MatchType matchType() const;
229 QRegularExpression::MatchOptions matchOptions() const;
232 friend class QRegularExpression;
234 QRegularExpressionMatchIterator(QRegularExpressionMatchIteratorPrivate &dd);
235 QSharedDataPointer<QRegularExpressionMatchIteratorPrivate> d;
238 Q_DECLARE_TYPEINFO(QRegularExpressionMatchIterator, Q_MOVABLE_TYPE);
244 #endif // QT_NO_REGEXP
246 #endif // QREGULAREXPRESSION_H