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
src_corelib_text_qregularexpression.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 Giuseppe D'Angelo <dangelog@gmail.com>.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QString>
5#include <QStringList>
6#include <QRegularExpression>
7#include <QRegularExpressionMatch>
8#include <QRegularExpressionMatchIterator>
9
10int main() {
11
12 {
13 //! [0]
14 QRegularExpression re("a pattern");
15 //! [0]
16 }
17
18 {
19 //! [1]
20 QRegularExpression re;
21 re.setPattern("another pattern");
22 //! [1]
23 }
24
25 {
26 //! [2]
27 // matches two digits followed by a space and a word
28 QRegularExpression re("\\d\\d \\w+");
29
30 // matches a backslash
31 QRegularExpression re2("\\\\");
32 //! [2]
33 }
34
35 {
36 //! [3]
37 QRegularExpression re("a third pattern");
38 QString pattern = re.pattern(); // pattern == "a third pattern"
39 //! [3]
40 }
41
42 {
43 //! [4]
44 // matches "Qt rocks", but also "QT rocks", "QT ROCKS", "qT rOcKs", etc.
45 QRegularExpression re("Qt rocks", QRegularExpression::CaseInsensitiveOption);
46 //! [4]
47 }
48
49 {
50 //! [5]
51 QRegularExpression re("^\\d+$");
52 re.setPatternOptions(QRegularExpression::MultilineOption);
53 // re matches any line in the subject string that contains only digits (but at least one)
54 //! [5]
55 }
56
57 {
58 //! [6]
59 QRegularExpression re = QRegularExpression("^two.*words$", QRegularExpression::MultilineOption
60 | QRegularExpression::DotMatchesEverythingOption);
61
62 QRegularExpression::PatternOptions options = re.patternOptions();
63 // options == QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption
64 //! [6]
65 }
66
67 {
68 //! [7]
69 // match two digits followed by a space and a word
70 QRegularExpression re("\\d\\d \\w+");
71 QRegularExpressionMatch match = re.match("abc123 def");
72 bool hasMatch = match.hasMatch(); // true
73 //! [7]
74 }
75
76 {
77 //! [8]
78 QRegularExpression re("\\d\\d \\w+");
79 QRegularExpressionMatch match = re.match("abc123 def");
80 if (match.hasMatch()) {
81 QString matched = match.captured(0); // matched == "23 def"
82 // ...
83 }
84 //! [8]
85 }
86
87 {
88 //! [9]
89 QRegularExpression re("\\d\\d \\w+");
90 QRegularExpressionMatch match = re.match("12 abc 45 def", 1);
91 if (match.hasMatch()) {
92 QString matched = match.captured(0); // matched == "45 def"
93 // ...
94 }
95 //! [9]
96 }
97
98 {
99 //! [10]
100 QRegularExpression re("^(\\d\\d)/(\\d\\d)/(\\d\\d\\d\\d)$");
101 QRegularExpressionMatch match = re.match("08/12/1985");
102 if (match.hasMatch()) {
103 QString day = match.captured(1); // day == "08"
104 QString month = match.captured(2); // month == "12"
105 QString year = match.captured(3); // year == "1985"
106 // ...
107 }
108 //! [10]
109 }
110
111 {
112 //! [11]
113 QRegularExpression re("abc(\\d+)def");
114 QRegularExpressionMatch match = re.match("XYZabc123defXYZ");
115 if (match.hasMatch()) {
116 int startOffset = match.capturedStart(1); // startOffset == 6
117 int endOffset = match.capturedEnd(1); // endOffset == 9
118 // ...
119 }
120 //! [11]
121 }
122
123 {
124 //! [12]
125 QRegularExpression re("^(?<date>\\d\\d)/(?<month>\\d\\d)/(?<year>\\d\\d\\d\\d)$");
126 QRegularExpressionMatch match = re.match("08/12/1985");
127 if (match.hasMatch()) {
128 QString date = match.captured("date"); // date == "08"
129 QString month = match.captured("month"); // month == "12"
130 QString year = match.captured("year"); // year == 1985
131 }
132 //! [12]
133 }
134
135 {
136 //! [13]
137 QRegularExpression re("(\\w+)");
138 QRegularExpressionMatchIterator i = re.globalMatch("the quick fox");
139 //! [13]
140
141 //! [14]
142 QStringList words;
143 while (i.hasNext()) {
144 QRegularExpressionMatch match = i.next();
145 QString word = match.captured(1);
146 words << word;
147 }
148 // words contains "the", "quick", "fox"
149 //! [14]
150 }
151
152 {
153 //! [15]
154 QString pattern("^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d\\d?, \\d\\d\\d\\d$");
155 QRegularExpression re(pattern);
156
157 QString input("Jan 21,");
158 QRegularExpressionMatch match = re.match(input, 0, QRegularExpression::PartialPreferCompleteMatch);
159 bool hasMatch = match.hasMatch(); // false
160 bool hasPartialMatch = match.hasPartialMatch(); // true
161 //! [15]
162 }
163
164 {
165 QString pattern("^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d\\d?, \\d\\d\\d\\d$");
166 QRegularExpression re(pattern);
167 //! [16]
168 QString input("Dec 8, 1985");
169 QRegularExpressionMatch match = re.match(input, 0, QRegularExpression::PartialPreferCompleteMatch);
170 bool hasMatch = match.hasMatch(); // true
171 bool hasPartialMatch = match.hasPartialMatch(); // false
172 //! [16]
173 }
174
175 {
176 //! [17]
177 QRegularExpression re("abc\\w+X|def");
178 QRegularExpressionMatch match = re.match("abcdef", 0, QRegularExpression::PartialPreferCompleteMatch);
179 bool hasMatch = match.hasMatch(); // true
180 bool hasPartialMatch = match.hasPartialMatch(); // false
181 QString captured = match.captured(0); // captured == "def"
182 //! [17]
183 }
184
185 {
186 //! [18]
187 QRegularExpression re("abc\\w+X|defY");
188 QRegularExpressionMatch match = re.match("abcdef", 0, QRegularExpression::PartialPreferCompleteMatch);
189 bool hasMatch = match.hasMatch(); // false
190 bool hasPartialMatch = match.hasPartialMatch(); // true
191 QString captured = match.captured(0); // captured == "abcdef"
192 //! [18]
193 }
194
195 {
196 //! [19]
197 QRegularExpression re("abc|ab");
198 QRegularExpressionMatch match = re.match("ab", 0, QRegularExpression::PartialPreferFirstMatch);
199 bool hasMatch = match.hasMatch(); // false
200 bool hasPartialMatch = match.hasPartialMatch(); // true
201 //! [19]
202 }
203
204 {
205 //! [20]
206 QRegularExpression re("abc(def)?");
207 QRegularExpressionMatch match = re.match("abc", 0, QRegularExpression::PartialPreferFirstMatch);
208 bool hasMatch = match.hasMatch(); // false
209 bool hasPartialMatch = match.hasPartialMatch(); // true
210 //! [20]
211 }
212
213 {
214 //! [21]
215 QRegularExpression re("(abc)*");
216 QRegularExpressionMatch match = re.match("abc", 0, QRegularExpression::PartialPreferFirstMatch);
217 bool hasMatch = match.hasMatch(); // false
218 bool hasPartialMatch = match.hasPartialMatch(); // true
219 //! [21]
220 }
221
222 {
223 //! [22]
224 QRegularExpression invalidRe("(unmatched|parenthesis");
225 bool isValid = invalidRe.isValid(); // false
226 //! [22]
227 }
228
229 {
230 //! [23]
231 QRegularExpression invalidRe("(unmatched|parenthesis");
232 if (!invalidRe.isValid()) {
233 QString errorString = invalidRe.errorString(); // errorString == "missing )"
234 int errorOffset = invalidRe.patternErrorOffset(); // errorOffset == 22
235 // ...
236 }
237 //! [23]
238 }
239
240 {
241 //! [26]
242 QString escaped = QRegularExpression::escape("a(x) = f(x) + g(x)");
243 // escaped == "a\\‍(x\\‍)\\ \\=\\ f\\‍(x\\‍)\\ \\+\\ g\\‍(x\\‍)"
244 //! [26]
245 }
246
247 {
248 QString name;
249 QString nickname;
250 //! [27]
251 QString pattern = "(" + QRegularExpression::escape(name) +
252 "|" + QRegularExpression::escape(nickname) + ")";
253 QRegularExpression re(pattern);
254 //! [27]
255 }
256
257 {
258 QString string;
259 QRegularExpression re;
260 //! [28]
261 QRegularExpressionMatch match = re.match(string);
262 for (int i = 0; i <= match.lastCapturedIndex(); ++i) {
263 QString captured = match.captured(i);
264 // ...
265 }
266 //! [28]
267 }
268
269 {
270 //! [29]
271 QRegularExpression re("(\\d\\d) (?<name>\\w+)");
272 QRegularExpressionMatch match = re.match("23 Jordan");
273 if (match.hasMatch()) {
274 QString number = match.captured(1); // first == "23"
275 QString name = match.captured("name"); // name == "Jordan"
276 }
277 //! [29]
278 }
279
280 {
281 //! [30]
282 // extracts the words
283 QRegularExpression re("(\\w+)");
284 QString subject("the quick fox");
285 QRegularExpressionMatchIterator i = re.globalMatch(subject);
286 while (i.hasNext()) {
287 QRegularExpressionMatch match = i.next();
288 // ...
289 }
290 //! [30]
291 }
292
293 {
294 //! [31]
295 QString wildcard = QRegularExpression::wildcardToRegularExpression("*.jpeg");
296 // Will match files with names like:
297 // foo.jpeg
298 // f_o_o.jpeg
299 // föö.jpeg
300 //! [31]
301 }
302
303#if 0
304 //! [32]
305 (?<day>\d\d)-(?<month>\d\d)-(?<year>\d\d\d\d) (\w+) (?<name>\w+)
306 //! [32]
307
308 //! [33]
309 ("", "day", "month", "year", "", "name")
310 //! [33]
311#endif
312
313 {
314 //! [34]
315 // using a raw string literal, R"(raw_characters)", to be able to use "\w"
316 // without having to escape the backslash as "\\w"
317 QRegularExpression re(R"(\w+)");
318 QString subject("the quick fox");
319 for (const QRegularExpressionMatch &match : re.globalMatch(subject)) {
320 // ...
321 }
322 //! [34]
323 }
324
325 {
326 //! [35]
327 // matches two digits followed by a space and a word
328 QRegularExpression re(R"(\d\d \w+)");
329 //! [35]
330 }
331
332 {
333 //! [36]
334 QRegularExpression re("([a-z]+)|([A-Z]+)");
335 QRegularExpressionMatch m = re.match("UPPERCASE");
336 if (m.hasMatch()) {
337 qDebug() << m.hasCaptured(0); // true
338 qDebug() << m.hasCaptured(1); // false
339 qDebug() << m.hasCaptured(2); // true
340 }
341 //! [36]
342 }
343
344}
int main()
[open]