File: regex.so

package info (click to toggle)
lookup 1.08b-10
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 1,112 kB
  • ctags: 1,306
  • sloc: ansic: 12,638; makefile: 245; perl: 174; sh: 53
file content (356 lines) | stat: -rw-r--r-- 10,235 bytes parent folder | download | duplicates (9)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
.I "Regular expressions"
(\&\&regex\&\&for short)
are a\c
.Q code
used to indicate what kind of text you're looking for.
They're how one searches for things in the editors\c
.Q vi ",\c"
.Q stevie ",\c"
.Q mifes
etc., or with the grep commands.
There are differences among the various
regex flavors in use -- I'll describe the flavor used by
.I lookup
here. Also, in order to be clear for the common case, I might tell a few
lies, but nothing too heinous.

The regex\&\&a\&\&means\c
.Q "any line with an\&\&a\&\&in it."

Simple enough.

The regex\&\&ab\&\&means\&\&any line with an\&\&a\&\&immediately
followed by a\&\&b\&\&\&\&.  So the line
.nf
    I am feeling flabby
.fi
would\c
.Q match
the regex\&\&ab\&\&because, indeed, there's an\c
.Q ab
on that line. But it wouldn't match the line
.nf

    this line has no a followed _immediately_ by a b

.fi
because, well, what the lines says is true.

In most cases, letters and numbers in a regex just mean that you're looking
for those letters and numbers in the order given. However, there are some
special characters used within a regex.

A simple example would be a period. Rather than indicate that you're looking
for a period, it means\c
.Q "any character" ".  "
So the silly regex\&\&.\&\&would mean\c
.Q "any line that has any character on it."
Well, maybe not so silly... you can use it to find non-blank lines.

But more commonly it's used as part of a larger regex. Consider the
regex\&\&gray\&\&. It wouldn't match the line
.nf

    The sky was grey and cloudy.

.fi
because of the different spelling (grey vs. gray).
But the regex\&\&gr.y\&\&asks for\&\&any line with a\&\&g\&\&,\&\&r\&\&, some character, and then a\&\&y\&\&\&.
So this would get\c
.Q grey
and\c
.Q gray ".  "

A special construct somewhat similar to\&\&.\&\&would be the
.IR "character class" .
A character class starts with a\&\&[\&\&and ends with a\&\&]\&\&,
and will match any character given in between. An example might be
.nf

    gr[ea]y

.fi
which would match lines with a\&\&g\&\&,\&\&r\&\&,
an\&\&e\&\&\fIor\fP an\&\&a\&\&,
and then a\&\&y\&\&.
Inside a character class you can list as many
characters as you want to.

For example the simple regex\&\&x[0123456789]y\&\&would
match any line with a digit sandwiched between
an\&\&x\&\&and a\&\&y\&\&.

The order of the characters within the character class doesn't really
matter...\&\&[513467289]\&\&would be the same as\&\&[0123456789]\&\&.

But as a short cut, you could put\&\&[0\-9]\&\&instead
of\&\&[0123456789]\&\&.
So the character class\&\&[a\-z]\&\&would match any lower-case letter,
while the character class\&\&[a\-zA\-Z0\-9]\&\&would match any
letter or digit.

The character\&\&\-\&\&is special within a character class, but only if
it's not the first thing. Another character that's special in a character
class is\&\&^\&\&, if it
.I is
the first thing. It\c
.Q inverts
the class so that it will match any character
.I not
listed. The class\&\&[^a\-zA\-Z0\-9]\&\&would match any
line with spaces or punctuation on them.

There are some special short-hand sequences for some common character
classes. The sequence\&\&\\d\&\&means\c
.Q digit ", "
and is the same as\&\&[0\-9]\&\&. \&\&\\w\&\&means\c
.Q "word element"
and is the same as\&\&[0\-9a\-zA\-Z_]\&\&. \&\&\\s\&\&means\c
.Q "space-type thing"
and is the same as\&\&[ \\t]\&\&(\&\&\\t\&\&means tab).

You can also use\&\&\\D\&\&,\&\&\\W\&\&, and\&\&\\S\&\&to
mean things
.I not
a digit, word element, or space-type thing.

Another special character would be\&\&?\&\&. This means\c
.Q "maybe one of whatever was just before it, not is fine too" ".  "
In the regex \&\&bikes? for rent\&\&, the\c
.Q whatever
would be the\&\&s\&\&, so this would match lines
with either\c
.Q "bikes for rent"
or\c
.Q "bike for rent" ".  "


Parentheses are also special, and can group things together.
In the regex
.nf

big (fat harry)? deal

.fi
the\c
.Q whatever
for the\&\&?\&\&would be\c
.Q "fat harry" ".  "
But be careful to pay attention to details... this regex would match
.nf
    I don't see what the big fat harry deal is!
.fi
but
.I not
.nf
    I don't see what the big deal is!
.fi

That's because if you take away the\c
.Q whatever
of the\&\&?\&\&, you end up
with
.nf
    big  deal
.fi
Notice that there are
.I two
spaces between the words, and the regex didn't allow for that.
The regex to get either line above would be
.nf
    big (fat harry )?deal
.fi
or
.nf
    big( fat harry)? deal
.fi
Do you see how they're essentially the same?

Similar to\&\&?\&\&is\&\&*\&\&, which means\c
.Q "any number, including none, of whatever's right in front" ".  "
It more or less means that whatever is tagged with\&\&*\&\&is allowed,
but not required, so something like
.nf
    I (really )*hate peas
.fi
would match\c
.Q "I hate peas" ",\c"
.Q "I really hate peas!" ",\c"
.Q "I really really hate peas" ", "
etc.

Similar to both\&\&?\&\&and\&\&*\&\&is\&\&+\&\&, which means\c
.Q "at least one of whatever just in front, but more is fine too" ".  "
The regex\&\&mis+pelling\&\&would match\c
.Q "mi\fIs\fPpelling" ",\c"
.Q "mi\fIss\fPpelling" ",\c"
.Q "mi\fIsss\fPpelling" ", "
etc. Actually, it's just the same as\&\&miss*pelling\&\&but
more simple to type. The regex\&\&ss*\&\&means\&\&an\&\&s\&\&,
followed by zero or more\&\&s\&\&\&\&,
while\&\&s+\&\&means\c
.Q "one or more\&\&s\&\&" ".  "
Both really the same.

The special character\&\&|\&\&means\c
.Q or ".  "
Unlike\&\&+\&\&,\&\&*\&\&, and\&\&?\&\&which
act on the thing
.I immediately
before, the\&\&|\&\&is more\c
.Q global ".  "
.nf
    give me (this|that) one
.fi
Would match lines that had\c
.Q "give me this one"
or\c
.Q "give me that one"
in them.

You can even combine more than two:
.nf
    give me (this|that|the other) one
.fi

How about:
.nf
    [Ii]t is a (nice |sunny |bright |clear )*day
.fi

Here, the\c
.Q whatever
immediately before the\&\&*\&\&is
.nf
    (nice |sunny |bright |clear )
.fi
So this regex would
match all the following lines:
.nf
   \fIIt is a day\fP.
   I think \fIit is a nice day\fP.
   \fIIt is a clear sunny day\fP today.
   If \fIit is a clear sunny nice sunny sunny sunny bright day\fP then....
.fi
Notice how the\&\&[Ii]t\&\&matches either\c
.Q It
or\c
.Q it "?  "


Note that the above regex would also match
.nf
   fru\fIit is a day\fP
.fi
because it indeed fulfills all requirements of the regex, even though the\c
.Q it
is really part of the word\c
.Q fruit ".  "
To answer concerns like this, which are common,
are\&\&<\&\&and\&\&>\&\&, which mean\c
.Q "word break" ".  "
The regex\&\&<it\&\&would match any line with\c
.Q it
.IR "beginning a word" ", "
while\&\&it>\&\&would match any line with\c
.Q it
.IR "ending a word" ".  "
And, of course,\&\&<it>\&\&would match any line with
.I "the word\c"
.Q it
in it.

Going back to the regex to find grey/gray, that would make more
sense, then, as
.nf
    <gr[ae]y>
.fi
which would match only the
.I words\c
.Q grey
and\c
.Q gray ".  "

Somewhat similar are\&\&^\&\&and\&\&$\&\&,
which mean\c
.Q "beginning of line"
and\c
.Q "end of line" ", "
respectively (but, not in a character class, of course).
So the regex\&\&^fun\&\&would find any line that begins
with the letters\c
.Q fun ", "
while\&\&^fun>\&\&would find any line that begins with the
.I word\c
.Q fun ".  "
\&\&^fun$\&\&would find any line that was exactly\c
.Q fun ".  "


Finally,\&\&^\\s*fun\\s*$\&\&would match any line that\c
.Q fun
exactly, but perhaps also had leading and/or trailing whitespace.

That's pretty much it. There are more complex things, some of which
I'll mention in the list below, but even with these few simple constructs
one can specify very detailed and complex patterns.

Let's summarize some of the special things in regular expressions:
.nf

Items that are basic units:
  \fIchar\fP      any non-special character matches itself.
  \\\fIchar\fP     special chars, when proceeded by \\, become non-special.
  .         Matches any one character (except \\n).
  \\n        Newline
  \\t        Tab.
  \\r        Carriage Return.
  \\f        Formfeed.
  \\d        Digit. Just a short-hand for [0\-9].
  \\w        Word element. Just a short-hand for [0\-9a\-zA\-Z_].
  \\s        Whitespace. Just a short-hand for [\\t \\n\\r\\f].
  \\## \\###  Two or three digit octal number indicating a single byte.
  [\fIchars\fP]   Matches a character if it's one of the characters listed.
  [^\fIchars\fP]  Matches a character if it's not one of the ones listed.

  The \\\fIchar\fP items above can be used within a character class,
  but not the items below.

  \\D        Anything not \\d.
  \\W        Anything not \\w.
  \\S        Anything not \\s.
  \\a        Any ASCII character.
  \\A        Any multibyte character.
  \\k        Any (not half-width) katakana character (including ).
  \\K        Any character not \\k (except \\n).
  \\h        Any hiragana character.
  \\H        Any character not \\h (except \\n).
  (\fIregex\fP)   Parens make the \fIregex\fP one unit.
  (?:\fIregex\fP)   [from perl5] Grouping-only parens -- can't use for \\# (below)
  \\c        Any JISX0208 kanji (kuten rows 16-84)
  \\C        Any character not \\c (except \\n).
  \\#        Match whatever was matched by the #th paren from the left.

Withȡto indicate oneunitas above, the following may be used:

  ?       A  allowed, but not required.
  +       At least one  required, but more ok.
  *       Any number of  ok, but none required.

There are also ways to matchsituations:

  \\b        A word boundary.
  <         Same as \\b.
  >         Same as \\b.
  ^         Matches the beginning of the line.
  $         Matches the end of the line.

Finally, theoris

  \fIreg1\fP|\fIreg2\fP Match if either \fIreg1\fP or \fIreg2\fP match.

Note that\\kand the like aren't allowed in character classes, so
something such as[\\k\\h]to try to get all kana won't work.
Use (\\k|\\h)instead.

.fi