1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 Giuseppe D'Angelo <dangelog@gmail.com>.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50
51 #include <QString>
52 #include <QStringList>
53 #include <QRegularExpression>
54 #include <QRegularExpressionMatch>
55 #include <QRegularExpressionMatchIterator>
56
main()57 int main() {
58
59 {
60 //! [0]
61 QRegularExpression re("a pattern");
62 //! [0]
63 }
64
65 {
66 //! [1]
67 QRegularExpression re;
68 re.setPattern("another pattern");
69 //! [1]
70 }
71
72 {
73 //! [2]
74 // matches two digits followed by a space and a word
75 QRegularExpression re("\\d\\d \\w+");
76
77 // matches a backslash
78 QRegularExpression re2("\\\\");
79 //! [2]
80 }
81
82 {
83 //! [3]
84 QRegularExpression re("a third pattern");
85 QString pattern = re.pattern(); // pattern == "a third pattern"
86 //! [3]
87 }
88
89 {
90 //! [4]
91 // matches "Qt rocks", but also "QT rocks", "QT ROCKS", "qT rOcKs", etc.
92 QRegularExpression re("Qt rocks", QRegularExpression::CaseInsensitiveOption);
93 //! [4]
94 }
95
96 {
97 //! [5]
98 QRegularExpression re("^\\d+$");
99 re.setPatternOptions(QRegularExpression::MultilineOption);
100 // re matches any line in the subject string that contains only digits (but at least one)
101 //! [5]
102 }
103
104 {
105 //! [6]
106 QRegularExpression re = QRegularExpression("^two.*words$", QRegularExpression::MultilineOption
107 | QRegularExpression::DotMatchesEverythingOption);
108
109 QRegularExpression::PatternOptions options = re.patternOptions();
110 // options == QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption
111 //! [6]
112 }
113
114 {
115 //! [7]
116 // match two digits followed by a space and a word
117 QRegularExpression re("\\d\\d \\w+");
118 QRegularExpressionMatch match = re.match("abc123 def");
119 bool hasMatch = match.hasMatch(); // true
120 //! [7]
121 }
122
123 {
124 //! [8]
125 QRegularExpression re("\\d\\d \\w+");
126 QRegularExpressionMatch match = re.match("abc123 def");
127 if (match.hasMatch()) {
128 QString matched = match.captured(0); // matched == "23 def"
129 // ...
130 }
131 //! [8]
132 }
133
134 {
135 //! [9]
136 QRegularExpression re("\\d\\d \\w+");
137 QRegularExpressionMatch match = re.match("12 abc 45 def", 1);
138 if (match.hasMatch()) {
139 QString matched = match.captured(0); // matched == "45 def"
140 // ...
141 }
142 //! [9]
143 }
144
145 {
146 //! [10]
147 QRegularExpression re("^(\\d\\d)/(\\d\\d)/(\\d\\d\\d\\d)$");
148 QRegularExpressionMatch match = re.match("08/12/1985");
149 if (match.hasMatch()) {
150 QString day = match.captured(1); // day == "08"
151 QString month = match.captured(2); // month == "12"
152 QString year = match.captured(3); // year == "1985"
153 // ...
154 }
155 //! [10]
156 }
157
158 {
159 //! [11]
160 QRegularExpression re("abc(\\d+)def");
161 QRegularExpressionMatch match = re.match("XYZabc123defXYZ");
162 if (match.hasMatch()) {
163 int startOffset = match.capturedStart(1); // startOffset == 6
164 int endOffset = match.capturedEnd(1); // endOffset == 9
165 // ...
166 }
167 //! [11]
168 }
169
170 {
171 //! [12]
172 QRegularExpression re("^(?<date>\\d\\d)/(?<month>\\d\\d)/(?<year>\\d\\d\\d\\d)$");
173 QRegularExpressionMatch match = re.match("08/12/1985");
174 if (match.hasMatch()) {
175 QString date = match.captured("date"); // date == "08"
176 QString month = match.captured("month"); // month == "12"
177 QString year = match.captured("year"); // year == 1985
178 }
179 //! [12]
180 }
181
182 {
183 //! [13]
184 QRegularExpression re("(\\w+)");
185 QRegularExpressionMatchIterator i = re.globalMatch("the quick fox");
186 //! [13]
187
188 //! [14]
189 QStringList words;
190 while (i.hasNext()) {
191 QRegularExpressionMatch match = i.next();
192 QString word = match.captured(1);
193 words << word;
194 }
195 // words contains "the", "quick", "fox"
196 //! [14]
197 }
198
199 {
200 //! [15]
201 QString pattern("^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d\\d?, \\d\\d\\d\\d$");
202 QRegularExpression re(pattern);
203
204 QString input("Jan 21,");
205 QRegularExpressionMatch match = re.match(input, 0, QRegularExpression::PartialPreferCompleteMatch);
206 bool hasMatch = match.hasMatch(); // false
207 bool hasPartialMatch = match.hasPartialMatch(); // true
208 //! [15]
209 }
210
211 {
212 QString pattern("^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d\\d?, \\d\\d\\d\\d$");
213 QRegularExpression re(pattern);
214 //! [16]
215 QString input("Dec 8, 1985");
216 QRegularExpressionMatch match = re.match(input, 0, QRegularExpression::PartialPreferCompleteMatch);
217 bool hasMatch = match.hasMatch(); // true
218 bool hasPartialMatch = match.hasPartialMatch(); // false
219 //! [16]
220 }
221
222 {
223 //! [17]
224 QRegularExpression re("abc\\w+X|def");
225 QRegularExpressionMatch match = re.match("abcdef", 0, QRegularExpression::PartialPreferCompleteMatch);
226 bool hasMatch = match.hasMatch(); // true
227 bool hasPartialMatch = match.hasPartialMatch(); // false
228 QString captured = match.captured(0); // captured == "def"
229 //! [17]
230 }
231
232 {
233 //! [18]
234 QRegularExpression re("abc\\w+X|defY");
235 QRegularExpressionMatch match = re.match("abcdef", 0, QRegularExpression::PartialPreferCompleteMatch);
236 bool hasMatch = match.hasMatch(); // false
237 bool hasPartialMatch = match.hasPartialMatch(); // true
238 QString captured = match.captured(0); // captured == "abcdef"
239 //! [18]
240 }
241
242 {
243 //! [19]
244 QRegularExpression re("abc|ab");
245 QRegularExpressionMatch match = re.match("ab", 0, QRegularExpression::PartialPreferFirstMatch);
246 bool hasMatch = match.hasMatch(); // false
247 bool hasPartialMatch = match.hasPartialMatch(); // true
248 //! [19]
249 }
250
251 {
252 //! [20]
253 QRegularExpression re("abc(def)?");
254 QRegularExpressionMatch match = re.match("abc", 0, QRegularExpression::PartialPreferFirstMatch);
255 bool hasMatch = match.hasMatch(); // false
256 bool hasPartialMatch = match.hasPartialMatch(); // true
257 //! [20]
258 }
259
260 {
261 //! [21]
262 QRegularExpression re("(abc)*");
263 QRegularExpressionMatch match = re.match("abc", 0, QRegularExpression::PartialPreferFirstMatch);
264 bool hasMatch = match.hasMatch(); // false
265 bool hasPartialMatch = match.hasPartialMatch(); // true
266 //! [21]
267 }
268
269 {
270 //! [22]
271 QRegularExpression invalidRe("(unmatched|parenthesis");
272 bool isValid = invalidRe.isValid(); // false
273 //! [22]
274 }
275
276 {
277 //! [23]
278 QRegularExpression invalidRe("(unmatched|parenthesis");
279 if (!invalidRe.isValid()) {
280 QString errorString = invalidRe.errorString(); // errorString == "missing )"
281 int errorOffset = invalidRe.patternErrorOffset(); // errorOffset == 22
282 // ...
283 }
284 //! [23]
285 }
286
287 {
288 //! [24]
289 QString p("a .*|pattern");
290
291 // re matches exactly the pattern string p
292 QRegularExpression re(QRegularExpression::anchoredPattern(p));
293 //! [24]
294 }
295
296 {
297 //! [26]
298 QString escaped = QRegularExpression::escape("a(x) = f(x) + g(x)");
299 // escaped == "a\\(x\\)\\ \\=\\ f\\(x\\)\\ \\+\\ g\\(x\\)"
300 //! [26]
301 }
302
303 {
304 QString name;
305 QString nickname;
306 //! [27]
307 QString pattern = "(" + QRegularExpression::escape(name) +
308 "|" + QRegularExpression::escape(nickname) + ")";
309 QRegularExpression re(pattern);
310 //! [27]
311 }
312
313 {
314 QString string;
315 QRegularExpression re;
316 //! [28]
317 QRegularExpressionMatch match = re.match(string);
318 for (int i = 0; i <= match.lastCapturedIndex(); ++i) {
319 QString captured = match.captured(i);
320 // ...
321 }
322 //! [28]
323 }
324
325 {
326 //! [29]
327 QRegularExpression re("(\\d\\d) (?<name>\\w+)");
328 QRegularExpressionMatch match = re.match("23 Jordan");
329 if (match.hasMatch()) {
330 QString number = match.captured(1); // first == "23"
331 QString name = match.captured("name"); // name == "Jordan"
332 }
333 //! [29]
334 }
335
336 {
337 //! [30]
338 // extracts the words
339 QRegularExpression re("(\\w+)");
340 QString subject("the quick fox");
341 QRegularExpressionMatchIterator i = re.globalMatch(subject);
342 while (i.hasNext()) {
343 QRegularExpressionMatch match = i.next();
344 // ...
345 }
346 //! [30]
347 }
348
349 {
350 //! [31]
351 QString wildcard = QRegularExpression::wildcardToRegularExpression("*.jpeg");
352 // Will match files with names like:
353 // foo.jpeg
354 // f_o_o.jpeg
355 // föö.jpeg
356 //! [31]
357 }
358
359 //! [32]
360 (?<day>\d\d)-(?<month>\d\d)-(?<year>\d\d\d\d) (\w+) (?<name>\w+)
361 //! [32]
362
363 //! [33]
364 ("", "day", "month", "year", "", "name")
365 //! [33]
366
367 }
368