1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
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 //! [0]
52 QRegExp rx("(\\d+)");
53 QString str = "Offsets: 12 14 99 231 7";
54 QStringList list;
55 int pos = 0;
56 
57 while ((pos = rx.indexIn(str, pos)) != -1) {
58     list << rx.cap(1);
59     pos += rx.matchedLength();
60 }
61 // list: ["12", "14", "99", "231", "7"]
62 //! [0]
63 
64 
65 //! [1]
66 QRegExp rx("*.txt");
67 rx.setPatternSyntax(QRegExp::Wildcard);
68 rx.exactMatch("README.txt");        // returns true
69 rx.exactMatch("welcome.txt.bak");   // returns false
70 //! [1]
71 
72 
73 //! [2]
74 QRegExp rx("ro+m");
75 rx.setMinimal(true);
76 //! [2]
77 
78 
79 //! [3]
80 QRegExp mark("\\b"      // word boundary
81               "[Mm]ark" // the word we want to match
82             );
83 //! [3]
84 
85 
86 //! [4]
87 QRegExp rx("^\\d\\d?$");    // match integers 0 to 99
88 rx.indexIn("123");          // returns -1 (no match)
89 rx.indexIn("-6");           // returns -1 (no match)
90 rx.indexIn("6");            // returns 0 (matched at position 0)
91 //! [4]
92 
93 
94 //! [5]
95 QRegExp rx("^\\S+$");       // match strings without whitespace
96 rx.indexIn("Hello world");  // returns -1 (no match)
97 rx.indexIn("This_is-OK");   // returns 0 (matched at position 0)
98 //! [5]
99 
100 
101 //! [6]
102 QRegExp rx("\\b(mail|letter|correspondence)\\b");
103 rx.indexIn("I sent you an email");     // returns -1 (no match)
104 rx.indexIn("Please write the letter"); // returns 17
105 //! [6]
106 
107 
108 //! [7]
109 QString captured = rx.cap(1); // captured == "letter"
110 //! [7]
111 
112 
113 //! [8]
114 QRegExp rx("&(?!amp;)");      // match ampersands but not &amp;
115 QString line1 = "This & that";
116 line1.replace(rx, "&amp;");
117 // line1 == "This &amp; that"
118 QString line2 = "His &amp; hers & theirs";
119 line2.replace(rx, "&amp;");
120 // line2 == "His &amp; hers &amp; theirs"
121 //! [8]
122 
123 
124 //! [9]
125 QString str = "One Eric another Eirik, and an Ericsson. "
126               "How many Eiriks, Eric?";
127 QRegExp rx("\\b(Eric|Eirik)\\b"); // match Eric or Eirik
128 int pos = 0;    // where we are in the string
129 int count = 0;  // how many Eric and Eirik's we've counted
130 while (pos >= 0) {
131     pos = rx.indexIn(str, pos);
132     if (pos >= 0) {
133         ++pos;      // move along in str
134         ++count;    // count our Eric or Eirik
135     }
136 }
137 //! [9]
138 
139 
140 //! [10]
141 str = "The Qt Company Ltd\tqt.io\tFinland";
142 QString company, web, country;
143 rx.setPattern("^([^\t]+)\t([^\t]+)\t([^\t]+)$");
144 if (rx.indexIn(str) != -1) {
145     company = rx.cap(1);
146     web = rx.cap(2);
147     country = rx.cap(3);
148 }
149 //! [10]
150 
151 
152 //! [11]
153 QStringList field = str.split("\t");
154 //! [11]
155 
156 
157 //! [12]
158 QRegExp rx("*.html");
159 rx.setPatternSyntax(QRegExp::Wildcard);
160 rx.exactMatch("index.html");                // returns true
161 rx.exactMatch("default.htm");               // returns false
162 rx.exactMatch("readme.txt");                // returns false
163 //! [12]
164 
165 
166 //! [13]
167 QString str = "offsets: 1.23 .50 71.00 6.00";
168 QRegExp rx("\\d*\\.\\d+");    // primitive floating point matching
169 int count = 0;
170 int pos = 0;
171 while ((pos = rx.indexIn(str, pos)) != -1) {
172     ++count;
173     pos += rx.matchedLength();
174 }
175 // pos will be 9, 14, 18 and finally 24; count will end up as 4
176 //! [13]
177 
178 
179 //! [14]
180 QRegExp rx("(\\d+)(\\s*)(cm|inch(es)?)");
181 int pos = rx.indexIn("Length: 36 inches");
182 QStringList list = rx.capturedTexts();
183 // list is now ("36 inches", "36", " ", "inches", "es")
184 //! [14]
185 
186 
187 //! [15]
188 QRegExp rx("(\\d+)(?:\\s*)(cm|inch(?:es)?)");
189 int pos = rx.indexIn("Length: 36 inches");
190 QStringList list = rx.capturedTexts();
191 // list is now ("36 inches", "36", "inches")
192 //! [15]
193 
194 
195 //! [16]
196 QStringList list = rx.capturedTexts();
197 QStringList::iterator it = list.begin();
198 while (it != list.end()) {
199     myProcessing(*it);
200     ++it;
201 }
202 //! [16]
203 
204 
205 //! [17]
206 QRegExp rxlen("(\\d+)(?:\\s*)(cm|inch)");
207 int pos = rxlen.indexIn("Length: 189cm");
208 if (pos > -1) {
209     QString value = rxlen.cap(1); // "189"
210     QString unit = rxlen.cap(2);  // "cm"
211     // ...
212 }
213 //! [17]
214 
215 
216 //! [18]
217 QRegExp rx("/([a-z]+)/([a-z]+)");
218 rx.indexIn("Output /dev/null");   // returns 7 (position of /dev/null)
219 rx.pos(0);                        // returns 7 (position of /dev/null)
220 rx.pos(1);                        // returns 8 (position of dev)
221 rx.pos(2);                        // returns 12 (position of null)
222 //! [18]
223 
224 
225 //! [19]
226 s1 = QRegExp::escape("bingo");   // s1 == "bingo"
227 s2 = QRegExp::escape("f(x)");    // s2 == "f\\(x\\)"
228 //! [19]
229 
230 
231 //! [20]
232 QRegExp rx("(" + QRegExp::escape(name) +
233            "|" + QRegExp::escape(alias) + ")");
234 //! [20]
235