QtBase  v6.3.1
src_corelib_text_qregularexpression.cpp
Go to the documentation of this file.
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 
57 int main() {
58 
59 {
61 QRegularExpression re("a pattern");
63 }
64 
65 {
68 re.setPattern("another pattern");
70 }
71 
72 {
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("\\\\");
80 }
81 
82 {
84 QRegularExpression re("a third pattern");
85 QString pattern = re.pattern(); // pattern == "a third pattern"
87 }
88 
89 {
91 // matches "Qt rocks", but also "QT rocks", "QT ROCKS", "qT rOcKs", etc.
94 }
95 
96 {
98 QRegularExpression re("^\\d+$");
100 // re matches any line in the subject string that contains only digits (but at least one)
102 }
103 
104 {
108 
109 QRegularExpression::PatternOptions options = re.patternOptions();
110 // options == QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption
112 }
113 
114 {
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
121 }
122 
123 {
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 }
132 }
133 
134 {
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 }
143 }
144 
145 {
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 }
156 }
157 
158 {
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 }
168 }
169 
170 {
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 }
180 }
181 
182 {
184 QRegularExpression re("(\\w+)");
185 QRegularExpressionMatchIterator i = re.globalMatch("the quick fox");
187 
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"
197 }
198 
199 {
201 QString pattern("^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d\\d?, \\d\\d\\d\\d$");
203 
204 QString input("Jan 21,");
206 bool hasMatch = match.hasMatch(); // false
207 bool hasPartialMatch = match.hasPartialMatch(); // true
209 }
210 
211 {
212 QString pattern("^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d\\d?, \\d\\d\\d\\d$");
215 QString input("Dec 8, 1985");
217 bool hasMatch = match.hasMatch(); // true
218 bool hasPartialMatch = match.hasPartialMatch(); // false
220 }
221 
222 {
224 QRegularExpression re("abc\\w+X|def");
226 bool hasMatch = match.hasMatch(); // true
227 bool hasPartialMatch = match.hasPartialMatch(); // false
228 QString captured = match.captured(0); // captured == "def"
230 }
231 
232 {
234 QRegularExpression re("abc\\w+X|defY");
236 bool hasMatch = match.hasMatch(); // false
237 bool hasPartialMatch = match.hasPartialMatch(); // true
238 QString captured = match.captured(0); // captured == "abcdef"
240 }
241 
242 {
244 QRegularExpression re("abc|ab");
246 bool hasMatch = match.hasMatch(); // false
247 bool hasPartialMatch = match.hasPartialMatch(); // true
249 }
250 
251 {
253 QRegularExpression re("abc(def)?");
255 bool hasMatch = match.hasMatch(); // false
256 bool hasPartialMatch = match.hasPartialMatch(); // true
258 }
259 
260 {
262 QRegularExpression re("(abc)*");
264 bool hasMatch = match.hasMatch(); // false
265 bool hasPartialMatch = match.hasPartialMatch(); // true
267 }
268 
269 {
271 QRegularExpression invalidRe("(unmatched|parenthesis");
272 bool isValid = invalidRe.isValid(); // false
274 }
275 
276 {
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 }
285 }
286 
287 {
289 QString escaped = QRegularExpression::escape("a(x) = f(x) + g(x)");
290 // escaped == "a\\‍(x\\‍)\\ \\=\\ f\\‍(x\\‍)\\ \\+\\ g\\‍(x\\‍)"
292 }
293 
294 {
295 QString name;
296 QString nickname;
299  "|" + QRegularExpression::escape(nickname) + ")";
302 }
303 
304 {
308 QRegularExpressionMatch match = re.match(string);
309 for (int i = 0; i <= match.lastCapturedIndex(); ++i) {
310  QString captured = match.captured(i);
311  // ...
312 }
314 }
315 
316 {
318 QRegularExpression re("(\\d\\d) (?<name>\\w+)");
319 QRegularExpressionMatch match = re.match("23 Jordan");
320 if (match.hasMatch()) {
321  QString number = match.captured(1); // first == "23"
322  QString name = match.captured("name"); // name == "Jordan"
323 }
325 }
326 
327 {
329 // extracts the words
330 QRegularExpression re("(\\w+)");
331 QString subject("the quick fox");
333 while (i.hasNext()) {
334  QRegularExpressionMatch match = i.next();
335  // ...
336 }
338 }
339 
340 {
343 // Will match files with names like:
344 // foo.jpeg
345 // f_o_o.jpeg
346 // föö.jpeg
348 }
349 
351  (?<day>\d\d)-(?<month>\d\d)-(?<year>\d\d\d\d) (\w+) (?<name>\w+)
353 
355  ("", "day", "month", "year", "", "name")
357 
358 {
360 // using a raw string literal, R"(raw_characters)", to be able to use "\w"
361 // without having to escape the backslash as "\\w"
362 QRegularExpression re(R"(\w+)");
363 QString subject("the quick fox");
364 for (const QRegularExpressionMatch &match : re.globalMatch(subject)) {
365  // ...
366 }
368 }
369 
370 {
372 // matches two digits followed by a space and a word
373 QRegularExpression re(R"(\d\d \w+)");
375 }
376 
377 {
379 QRegularExpression re("([a-z]+)|([A-Z]+)");
380 QRegularExpressionMatch m = re.match("UPPERCASE");
381 if (m.hasMatch()) {
382  qDebug() << m.hasCaptured(0); // true
383  qDebug() << m.hasCaptured(1); // false
384  qDebug() << m.hasCaptured(2); // true
385 }
387 }
388 
389 }
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
The QRegularExpression class provides pattern matching using regular expressions.
PatternOptions patternOptions() const
void setPatternOptions(PatternOptions options)
qsizetype patternErrorOffset() const
static QString escape(const QString &str)
void setPattern(const QString &pattern)
QRegularExpressionMatch match(const QString &subject, qsizetype offset=0, MatchType matchType=NormalMatch, MatchOptions matchOptions=NoMatchOption) const
QString errorString() const
QRegularExpressionMatchIterator globalMatch(const QString &subject, qsizetype offset=0, MatchType matchType=NormalMatch, MatchOptions matchOptions=NoMatchOption) const
static QString wildcardToRegularExpression(const QString &str, WildcardConversionOptions options=DefaultWildcardConversion)
The QRegularExpressionMatch class provides the results of a matching a QRegularExpression against a s...
qsizetype capturedEnd(int nth=0) const
qsizetype capturedStart(int nth=0) const
QString captured(int nth=0) const
The QRegularExpressionMatchIterator class provides an iterator on the results of a global match of a ...
The QString class provides a Unicode character string.
Definition: qstring.h:388
The QStringList class provides a list of strings.
QDate date
[1]
#define qDebug
[1]
Definition: qlogging.h:177
const GLfloat * m
GLfloat GLfloat GLfloat w
[0]
GLuint name
GLubyte * pattern
Definition: qopenglext.h:2744
GLenum GLenum GLenum input
Definition: qopenglext.h:10816
GLsizei const GLchar *const * string
[0]
Definition: qopenglext.h:694