Qt 6.x
The Qt SDK
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{
14QRegularExpression re("a pattern");
16}
17
18{
21re.setPattern("another pattern");
23}
24
25{
27// matches two digits followed by a space and a word
28QRegularExpression re("\\d\\d \\w+");
29
30// matches a backslash
31QRegularExpression re2("\\\\");
33}
34
35{
37QRegularExpression re("a third pattern");
38QString pattern = re.pattern(); // pattern == "a third pattern"
40}
41
42{
44// matches "Qt rocks", but also "QT rocks", "QT ROCKS", "qT rOcKs", etc.
47}
48
49{
51QRegularExpression re("^\\d+$");
53// re matches any line in the subject string that contains only digits (but at least one)
55}
56
57{
61
62QRegularExpression::PatternOptions options = re.patternOptions();
63// options == QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption
65}
66
67{
69// match two digits followed by a space and a word
70QRegularExpression re("\\d\\d \\w+");
71QRegularExpressionMatch match = re.match("abc123 def");
72bool hasMatch = match.hasMatch(); // true
74}
75
76{
78QRegularExpression re("\\d\\d \\w+");
79QRegularExpressionMatch match = re.match("abc123 def");
80if (match.hasMatch()) {
81 QString matched = match.captured(0); // matched == "23 def"
82 // ...
83}
85}
86
87{
89QRegularExpression re("\\d\\d \\w+");
90QRegularExpressionMatch match = re.match("12 abc 45 def", 1);
91if (match.hasMatch()) {
92 QString matched = match.captured(0); // matched == "45 def"
93 // ...
94}
96}
97
98{
100QRegularExpression re("^(\\d\\d)/(\\d\\d)/(\\d\\d\\d\\d)$");
101QRegularExpressionMatch match = re.match("08/12/1985");
102if (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}
109}
110
111{
113QRegularExpression re("abc(\\d+)def");
114QRegularExpressionMatch match = re.match("XYZabc123defXYZ");
115if (match.hasMatch()) {
116 int startOffset = match.capturedStart(1); // startOffset == 6
117 int endOffset = match.capturedEnd(1); // endOffset == 9
118 // ...
119}
121}
122
123{
125QRegularExpression re("^(?<date>\\d\\d)/(?<month>\\d\\d)/(?<year>\\d\\d\\d\\d)$");
126QRegularExpressionMatch match = re.match("08/12/1985");
127if (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}
133}
134
135{
137QRegularExpression re("(\\w+)");
138QRegularExpressionMatchIterator i = re.globalMatch("the quick fox");
140
142QStringList words;
143while (i.hasNext()) {
145 QString word = match.captured(1);
146 words << word;
147}
148// words contains "the", "quick", "fox"
150}
151
152{
154QString pattern("^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d\\d?, \\d\\d\\d\\d$");
156
157QString input("Jan 21,");
159bool hasMatch = match.hasMatch(); // false
160bool hasPartialMatch = match.hasPartialMatch(); // true
162}
163
164{
165QString pattern("^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d\\d?, \\d\\d\\d\\d$");
168QString input("Dec 8, 1985");
170bool hasMatch = match.hasMatch(); // true
171bool hasPartialMatch = match.hasPartialMatch(); // false
173}
174
175{
177QRegularExpression re("abc\\w+X|def");
179bool hasMatch = match.hasMatch(); // true
180bool hasPartialMatch = match.hasPartialMatch(); // false
181QString captured = match.captured(0); // captured == "def"
183}
184
185{
187QRegularExpression re("abc\\w+X|defY");
189bool hasMatch = match.hasMatch(); // false
190bool hasPartialMatch = match.hasPartialMatch(); // true
191QString captured = match.captured(0); // captured == "abcdef"
193}
194
195{
197QRegularExpression re("abc|ab");
199bool hasMatch = match.hasMatch(); // false
200bool hasPartialMatch = match.hasPartialMatch(); // true
202}
203
204{
206QRegularExpression re("abc(def)?");
208bool hasMatch = match.hasMatch(); // false
209bool hasPartialMatch = match.hasPartialMatch(); // true
211}
212
213{
215QRegularExpression re("(abc)*");
217bool hasMatch = match.hasMatch(); // false
218bool hasPartialMatch = match.hasPartialMatch(); // true
220}
221
222{
224QRegularExpression invalidRe("(unmatched|parenthesis");
225bool isValid = invalidRe.isValid(); // false
227}
228
229{
231QRegularExpression invalidRe("(unmatched|parenthesis");
232if (!invalidRe.isValid()) {
233 QString errorString = invalidRe.errorString(); // errorString == "missing )"
234 int errorOffset = invalidRe.patternErrorOffset(); // errorOffset == 22
235 // ...
236}
238}
239
240{
242QString escaped = QRegularExpression::escape("a(x) = f(x) + g(x)");
243// escaped == "a\\‍(x\\‍)\\ \\=\\ f\\‍(x\\‍)\\ \\+\\ g\\‍(x\\‍)"
245}
246
247{
249QString nickname;
252 "|" + QRegularExpression::escape(nickname) + ")";
255}
256
257{
262for (int i = 0; i <= match.lastCapturedIndex(); ++i) {
263 QString captured = match.captured(i);
264 // ...
265}
267}
268
269{
271QRegularExpression re("(\\d\\d) (?<name>\\w+)");
272QRegularExpressionMatch match = re.match("23 Jordan");
273if (match.hasMatch()) {
274 QString number = match.captured(1); // first == "23"
275 QString name = match.captured("name"); // name == "Jordan"
276}
278}
279
280{
282// extracts the words
283QRegularExpression re("(\\w+)");
284QString subject("the quick fox");
286while (i.hasNext()) {
288 // ...
289}
291}
292
293{
296// Will match files with names like:
297// foo.jpeg
298// f_o_o.jpeg
299// föö.jpeg
301}
302
304 (?<day>\d\d)-(?<month>\d\d)-(?<year>\d\d\d\d) (\w+) (?<name>\w+)
306
308 ("", "day", "month", "year", "", "name")
310
311{
313// using a raw string literal, R"(raw_characters)", to be able to use "\w"
314// without having to escape the backslash as "\\w"
315QRegularExpression re(R"(\w+)");
316QString subject("the quick fox");
317for (const QRegularExpressionMatch &match : re.globalMatch(subject)) {
318 // ...
319}
321}
322
323{
325// matches two digits followed by a space and a word
326QRegularExpression re(R"(\d\d \w+)");
328}
329
330{
332QRegularExpression re("([a-z]+)|([A-Z]+)");
333QRegularExpressionMatch m = re.match("UPPERCASE");
334if (m.hasMatch()) {
335 qDebug() << m.hasCaptured(0); // true
336 qDebug() << m.hasCaptured(1); // false
337 qDebug() << m.hasCaptured(2); // true
338}
340}
341
342}
\inmodule QtCore \reentrant
\inmodule QtCore \reentrant
\inmodule QtCore \reentrant
bool isValid() const
Returns true if the regular expression is a valid regular expression (that is, it contains no syntax ...
PatternOptions patternOptions() const
Returns the pattern options for the regular expression.
void setPatternOptions(PatternOptions options)
Sets the given options as the pattern options of the regular expression.
qsizetype patternErrorOffset() const
Returns the offset, inside the pattern string, at which an error was found when checking the validity...
static QString escape(const QString &str)
This is an overloaded member function, provided for convenience. It differs from the above function o...
void setPattern(const QString &pattern)
Sets the pattern string of the regular expression to pattern.
QString pattern() const
Returns the pattern string of the regular expression.
QRegularExpressionMatch match(const QString &subject, qsizetype offset=0, MatchType matchType=NormalMatch, MatchOptions matchOptions=NoMatchOption) const
Attempts to match the regular expression against the given subject string, starting at the position o...
QString errorString() const
Returns a textual description of the error found when checking the validity of the regular expression...
QRegularExpressionMatchIterator globalMatch(const QString &subject, qsizetype offset=0, MatchType matchType=NormalMatch, MatchOptions matchOptions=NoMatchOption) const
Attempts to perform a global match of the regular expression against the given subject string,...
static QString wildcardToRegularExpression(const QString &str, WildcardConversionOptions options=DefaultWildcardConversion)
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QDate date
[1]
#define qDebug
[1]
Definition qlogging.h:160
const GLfloat * m
GLfloat GLfloat GLfloat w
[0]
GLuint name
GLubyte * pattern
GLenum GLenum GLenum input
GLsizei const GLchar *const * string
[0]
Definition qopenglext.h:694
static bool match(const uchar *found, uint foundLen, const char *target, uint targetLen)