aboutsummaryrefslogtreecommitdiffstats
path: root/doc/codesnippets/doc/src/snippets/code/src_corelib_tools_qregexp.cpp
blob: c6f9a9dbc5f383d97a6b50922ee301f73ae6559f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//! [0]
rx = QRegExp("(\\d+)")
txt = "Offsets: 12 14 99 231 7"
lst = []

pos = rx.indexIn(txt, 0)

while pos != -1:
    lst.append(rx.cap(1))
    pos += rx.matchedLength()
    pos = rx.indexIn(txt, pos)

# lst: ["12", "14", "99", "231", "7"]
//! [0]


//! [1]
rx = QRegExp("*.txt")
rx.setPatternSyntax(QRegExp.Wildcard)
rx.exactMatch("README.txt")        # returns True
rx.exactMatch("welcome.txt.bak")   # returns False
//! [1]


//! [2]
rx = QRegExp("ro+m")
rx.setMinimal(True)
//! [2]


//! [3]
mark = QRegExp("\\b"       # word boundary
               "[Mm]ark"   # the word we want to match
              )
//! [3]


//! [4]
rx = QRegExp("^\\d\\d?$")  # match integers 0 to 99
rx.indexIn("123")          # returns -1 (no match)
rx.indexIn("-6")           # returns -1 (no match)
rx.indexIn("6")            # returns  0 (matched as position 0)
//! [4]


//! [5]
rx = QRegExp("^\\S+$")     # match strings without whitespace
rx.indexIn("Hello world")  # returns -1 (no match)
rx.indexIn("This_is-OK")   # returns  0 (matched at position 0)
//! [5]


//! [6]
rx = QRegExp("\\b(mail|letter|correspondence)\\b")
rx.indexIn("I sent you an email")     # returns -1 (no match)
rx.indexIn("Please write the letter") # returns 17
//! [6]


//! [7]
captured = rx.cap(1) # captured == "letter"
//! [7]


//! [8]
rx = QRegExp("&(?!amp;)")                  # match ampersands but not &
line1 = QString("This & that")
line1.replace(rx, "&")                 # line1 == "This & that"
line2 = QString("His & hers & theirs")
line2.replace(rx, "&")                 # line2 == "His & hers & theirs"
//! [8]


//! [9]
txt = QString("One Eric another Eirik, and an Ericsson. How many Eiriks, Eric?")
rx = QRegExp("\\b(Eric|Eirik)\\b")  # match Eric or Eirik
pos = 0                             # where we are in the string
count = 0                           # how many Eric and Eirik's we've counted

while pos >= 0:
    pos = rx.indexIn(txt, pos)
    if pos >= 0:
        pos += 1                    # move along in str
        count += 1                  # count our Eric or Eirik
//! [9]


//! [10]
txt = "Nokia Corporation and/or its subsidiary(-ies)\tqtsoftware.com\tNorway"
rx.setPattern("^([^\t]+)\t([^\t]+)\t([^\t]+)$");
if rx.indexIn(txt) != -1:
    company = rx.cap(1)
    web = rx.cap(2)
    country = rx.cap(3)
//! [10]


//! [11]
field = txt.split("\t")
//! [11]


//! [12]
rx = QRegExp("*.html")
rx.setPatternSyntax(QRegExp.Wildcard)
rx.exactMatch("index.html")             # returns True
rx.exactMatch("default.htm")            # returns False
rx.exactMatch("readme.txt")             # returns False
//! [12]


//! [13]
txt = QString("offsets: 1.23 .50 71.00 6.00")
rx = QRegExp("\\d*\\.\\d+")             # primitive floating point matching
count = 0
pos = rx.indexIn(txt, 0)
while pos != -1:
    count += 1
    pos += rx.matchedLength()
    pos = rx.indexIn(txt, pos)

# pos will be 9, 14, 18 and finally 24; count will end up as 4
//! [13]


//! [14]
rx = QRegExp("(\\d+)(\\s*)(cm|inch(es)?)")
pos = rx.indexIn("Length: 36 inches")
lst = rx.capturedTexts()
# lst is now ("36 inches", "36", " ", "inches", "es")
//! [14]


//! [15]
rx = QRegExp("(\\d+)(?:\\s*)(cm|inch(?:es)?)")
pos = rx.indexIn("Length: 36 inches")
lst = rx.capturedTexts()
# lst is now ("36 inches", "36", "inches")
//! [15]


//! [16]
for a in rx.capturedTexts():
    myProcessing(a)
//! [16]


//! [17]
rxlen  = QRegExp("(\\d+)(?:\\s*)(cm|inch)")
pos = rxlen.indexIn("Length: 189cm")
if pos > -1:
    value = rxlen.cap(1) # "189"
    unit = rxlen.cap(2)  # "cm"
//! [17]


//! [18]
rx = QRegExp("/([a-z]+)/([a-z]+)")
rx.indexIn("Output /dev/null")  # returns 7 (position of /dev/null)
rx.pos(0)                       # returns 7 (position of /dev/null)
rx.pos(1)                       # returns 8 (position of dev)
rx.pos(2)                       # returns 12 (position of null)
//! [18]


//! [19]
s1 = QRegExp.escape("bingo")    # s1 == "bingo"
s2 = QRegExp.escape("f(x)")     # s2 == "f\\(x\\)"
//! [19]


//! [20]
rx = QRegExp("(" + QRegExp.escape(name) + "|" + QRegExp.escape(alias) + ")")
//! [20]