summaryrefslogtreecommitdiff
path: root/support/w2latex/EXEMPLES/All_About_Grep.rtf
blob: 53db44d3de5932e6cf7c27ab28dd26145bed8458 (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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
{\rtf1\mac\deff2 {\fonttbl{\f0\fswiss Chicago;}{\f2\froman New York;}{\f3\fswiss Geneva;}{\f4\fmodern Monaco;}{\f20\froman Times;}{\f21\fswiss Helvetica;}{\f22\fmodern Courier;}{\f23\ftech Symbol;}{\f200\fnil MitreLogo;}{\f201\fnil MitreSymbol;}
{\f2222\fnil MITRE;}{\f14988\fnil BI Garamond BoldItalic;}{\f14989\fnil B Garamond Bold;}{\f14990\fnil I Garamond LightItalic;}{\f14991\fnil Garamond;}{\f15018\fnil BI Palatino BoldItalic;}{\f15019\fnil B Palatino Bold;}{\f15020\fnil I Palatino Italic;}
{\f15021\fnil Palatino;}}{\colortbl\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;}{\stylesheet{\s243\tqc\tx4320\tqr\tx8640 
\f156 \sbasedon0\snext243 footer;}{\s244\tqc\tx4320\tqr\tx8640 \f156 \sbasedon0\snext244 header;}{\f156 \sbasedon222\snext0 Normal;}{\s1\fi720 \f156 \sbasedon0\snext1 Description Paragraph;}{\s2\li900\ri720 \f156 \sbasedon0\snext2 Item;}{\s3 \f156\fs36 
\sbasedon0\snext3 Chapter Header;}}{\info{\author Rich Siegel}}\margl1440\margr2160\widowctrl\ftnbj\ftnrestart \sectd \linemod0\linex0\cols1\colsx0\endnhere {\header \pard\plain \li180\ri-20\tx7200\tx9619 \f156 {\f20\fs20 \par 
}}{\footer \pard\plain \li180\ri-20\tx7200\tx9619 \f156 {\f20\fs20 \par 
}}\pard\plain \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 \f156 {\f21 ____________________________________________________________\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\b\f21\fs28 INTRODUCTION TO GREP\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f21 ____________________________________________________________\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\b\f20\fs20 grep}{\f20\fs20  is a a method of pattern matching that derives from the Unix\'aa system.  You are probably familiar with simpl
e pattern matching from using word processors;  when you ask a word processor to find all instances of the word "black", it is performing a simple pattern match, where each letter has to match literally.  Matching strings in this manner is not very hard.
\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 
But the ability to match strings in a more general manner is both more powerful and more complicated.  It allows for sophisticated pattern matching operations, such as matching all words that begin with the letter "P" and end with the letters "er", or dele
ting the first word of every line.  Grep provides a powerful means of doing this.\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
\par 
\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f21 \page ____________________________________________________________\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\b\f21\fs28 HOW GREP WORKS\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f21 ____________________________________________________________\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 
The "grep" mode of searching and replacing is a powerful tool.  At the expense of being somewhat slower than normal text searching, using grep allows the user to search for one of a set of many strings instead of a particular string.  As a simple example, 
you can search for any occurrence of an identifier beginning with the letter }{\f22\fs20 P}{\f20\fs20 , or all lines that begin with a left brace.\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 A }{\b\f20\fs20 pattern}{\f20\fs20 
 is a string of characters that, in turn, describes a set of strings of characters.  An example of a set of strings is the set of all strings that begin with the letter }{\f22\fs20 P}{\f20\fs20  and end with the letter }{\f22\fs20 r}{\f20\fs20 
;  the strings "Ptr" and "ProcPtr" are members of this set.  We say that a string is }{\b\f20\fs20 matched}{\f20\fs20  by a pattern if it is a member of the set described by the pattern.  Patterns are composed of sub-patterns which are patte
rns in themselves;  this is how complicated patterns may be formed.\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
Some examples of grep patterns:\par 
\par 
To replace a Pascal comment with a C comment, you would use \par 
}{\f22\fs20 \tab \{\\([^\}]*\\)\}\par 
}{\f20\fs20 to match the comment and\par 
}{\f22\fs20 \tab /*\\1*/\par 
}{\f20\fs20 to replace it.\par 
To change all words that begin with the letter}{\f22\fs20  P }{\f20\fs20 to begin with the letter}{\f22\fs20  Q}{\f20\fs20 , you would use\par 
}{\f22\fs20 \tab \\<P\\([A-Za-z0-9]*\\)\\>\par 
}{\f20\fs20 to match the word and\par 
}{\f22\fs20 \tab Q\\1\par 
}{\f20\fs20 to replace it.\par 
\par 
To change a list of names; ie:\par 
}{\f22\fs20 \tab FrameRect\par 
\tab PaintRect\par 
\tab EmptyRect\par 
\par 
}{\f20\fs20 to a list of names, followed by strings containing those names; i.e.}{\f22\fs20 \par 
\tab FrameRect,\tab "FrameRect",\par 
\tab PaintRect,\tab "PaintRect",\par 
\tab EmptyRect,\tab "EmptyRect",\par 
\par 
}{\f20\fs20 you would use\par 
}{\f22\fs20 \tab \\([A-Za-z][A-Za-z]*\\)\par 
}{\f20\fs20 to match the name and\par 
}{\f22\fs20 \tab \\1,\tab "\\1",\par 
}{\f20\fs20 to replace it.\par 
\par 
\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 
You don't have to understand how these work now;  in fact, it would be surprising if you do.  The following section goes through the grep pattern matching and replacement rules step by step, so that by the end of it you should be able to understand how eac
h of these grep patterns works and be able to make your own.\page \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f21 ____________________________________________________________\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\b\f21\fs28 PATTERN MATCHING AND REPLACEMENT RULES\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f21 ____________________________________________________________\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\b\f20\fs20 A note on notation:}{\f20\fs20 
  Writing about patterns and strings can be very confusing, since patterns and strings are made up of characters, as is this text.  Therefore, we use certain typographical conventions to distinguish various usages.\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 All literal characters will be in the courier font;  therefore,}{\f22\fs20  a }{\f20\fs20 and}{\f22\fs20  xyz }{\f20\fs20 
refer to those literal strings of characters.\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 All patterns, when talked about in the abstract, will be italicized;  therefore, }{\i\f20\fs20 p}{\f20\fs20  and }{\i\f20\fs20 q}{\f20\fs20 
 refer to abstract patterns.\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 All strings, when talked about in the abstract, will be Greek letters;  therefore, \'a7 and \'b5 refer to abstract strings.\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 
Sometimes we will be referring to parts of strings or patterns within longer ones.  In these cases, the parts that are being referred to will be underlined.  Therefore, in the string}{\f22\fs20  xx}{\f22\fs20\ul aab}{\f22\fs20 x}{\f20\fs20 
, only the sub-string}{\f22\fs20  aab }{\f20\fs20 is actually being referred to;  the other letters are used for context.\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 In the examples, a string that  can occur anywhere in a line will be preceded and followed by an ellipsis (}{\f22\fs20 \'c9)}{\f20\fs20 ; i.e. }{\f22\fs20 
\'c9xyz\'c9}{\f20\fs20 .  If it can occur only at the beginning of the line, it will only be followed by an ellipsis;  i.e., }{\f22\fs20 xyz\'c9}{\f20\fs20 
.  Similarly, if it can occur only at the end of the line, it will be preceded but not followed by an ellipsis.\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 In some cases, the state of case sensitivity affects the results of a pattern match.  In the examples we have noted when this is this case.\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\b\f21\fs28\ul Pattern matching}{\f20\fs28 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\b\f21 Simple matching}{\f21 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
1. Any character, with certain exceptions described below, is a pattern that matches itself.\par 
\par 
}{\b\f20\fs20 Examples:\par 
}{\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx9619 {\f20\fs20 \tab }{\f20\fs20\ul Pattern}{\f20\fs20 \tab \tab }{\f20\fs20\ul Text}{\f20\fs20 \tab }{\f20\fs20\ul With case sensitivity}{\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f22\fs20 X}{\f20\fs20 \tab matches\tab }{\f22\fs20 \'c9X\'c9\tab \par 
}{\f20\fs20 \tab doesn't match\tab }{\f22\fs20 \'c9x\'c9\tab }{\f20\fs20 on}{\f22\fs20 \par 
}{\f20\fs20 \tab but matches\tab }{\f22\fs20 \'c9x\'c9\tab }{\f20\fs20 off\par 
\par 
\par 
\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 2.}{\i\f20\fs20 \tab }{\f20\fs20 A pattern }{\i\f20\fs20 x}{\f20\fs20  followed by a pattern }{\i\f20\fs20 y}{\f20\fs20  forms a pattern }{\i\f20\fs20 xy}{
\f20\fs20  that matches any string \'a7\'b5 where \'a7 can be matched by }{\i\f20\fs20 x}{\f20\fs20  and \'b5 can be matched by }{\i\f20\fs20 y}{\f20\fs20 .  We can, of course, take the compound pattern }{\i\f20\fs20 xy}{\f20\fs20 
 and concatenate yet another pattern }{\i\f20\fs20 z}{\f20\fs20  onto it, forming the pattern }{\i\f20\fs20 xyz}{\f20\fs20 .\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
}{\b\f20\fs20 Examples:\par 
}{\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \tab }{\f20\fs20\ul Pattern}{\f20\fs20 \tab \tab \tab }{\f20\fs20\ul Text}{\f20\fs20 \tab }{\f20\fs20\ul With case sensitivity}{\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \tab }{\f22\fs20 XY}{\f20\fs20 \tab matches\tab }{\f22\fs20 \'c9XY\'c9\par 
}{\f20\fs20 \par 
\tab }{\f22\fs20 Ptr}{\f20\fs20 \tab matches\tab }{\f22\fs20 \'c9Ptr\'c9}{\f20\fs20 \par 
\tab \tab doesn't match\tab }{\f22\fs20 \'c9ptr\'c9\tab }{\f20\fs20 on}{\f22\fs20 \par 
}{\f20\fs20 \tab \tab but does match\tab }{\f22\fs20 \'c9ptr\'c9\tab }{\f20\fs20 off}{\f22\fs20 \par 
}{\f20\fs20 \par 
\par 
\par 
3.\tab The character}{\f22\fs20  . }{\f20\fs20 is a pattern that will match any character.\par 
\par 
\tab }{\b\f20\fs20 Examples:\par 
}{\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \tab }{\f20\fs20\ul Pattern}{\f20\fs20 \tab \tab \tab }{\f20\fs20\ul Text}{\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \tab }{\f22\fs20 P.r}{\f20\fs20 \tab matches\tab }{\f22\fs20 \'c9Ptr\'c9\tab \par 
\tab \tab }{\f20\fs20 and matches\tab }{\f22\fs20 \'c9P.r\'c9\tab }{\f20\fs20 \par 
}{\f22\fs20 \par 
}{\f20\fs20 \tab }{\f22\fs20 ..}{\f20\fs20 \tab matches\tab }{\f22\fs20 \'c9ab\'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
\tab \tab }{\f20\fs20 and matches\tab }{\f22\fs20 \'c9a.\'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
\par 
}{\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 4.\tab The character}{\f22\fs20  \\ }{\f20\fs20 followed by any character except}{\f22\fs20  (}{\f20\fs20 ,}{\f22\fs20  )}{\f20\fs20 ,}{\f22\fs20  <}{
\f20\fs20 ,}{\f22\fs20  >}{\f20\fs20 ,}{\f22\fs20  }{\f20\fs20 or one of the digits }{\f22\fs20 1}{\f20\fs20 -}{\f22\fs20 9}{\f20\fs20  is a pattern that matches that character.\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
\tab }{\b\f20\fs20 Examples:\par 
}{\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \tab }{\f20\fs20\ul Pattern}{\f20\fs20 \tab \tab \tab }{\f20\fs20\ul Text}{\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \tab }{\f22\fs20 P\\.r}{\f20\fs20 \tab matches\tab }{\f22\fs20 \'c9P.r\'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
\tab \tab }{\f20\fs20 but doesn't match}{\f22\fs20 \'c9Ptr\'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
\par 
}{\f20\fs20 \tab }{\f22\fs20 P\\\\r}{\f20\fs20 \tab matches\tab }{\f22\fs20 \'c9P\\r\'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
}{\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 5.\tab A string of characters }{\i\f20\fs20 s}{\f20\fs20  surrounded by square brackets (}{\f22\fs20 [ }{\f20\fs20 and a}{\f22\fs20  ]) }{\f20\fs20 
forms a pattern}{\f22\fs20  [}{\i\f22\fs20 s}{\f22\fs20 ] }{\f20\fs20 that matches a single instance of one of the characters in the string }{\i\f20\fs20 s}{\f20\fs20 .  Note that the case sensitivity flag does }{\f20\fs20\ul not}{\f20\fs20 
 apply to characters between square brackets:  letters must match exactly.\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
\tab }{\b\f20\fs20 Examples:\par 
}{\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \tab }{\f20\fs20\ul Pattern}{\f20\fs20 \tab \tab \tab \tab }{\f20\fs20\ul Text}{\f20\fs20 \par 
\tab }{\f22\fs20 [abc]}{\f20\fs20 \tab matches\tab \tab }{\f22\fs20 \'c9}{\f22\fs20\ul a}{\f22\fs20 b\'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
\tab \tab }{\f20\fs20 and matches\tab \tab }{\f22\fs20 \'c9x}{\f22\fs20\ul b}{\f22\fs20 \'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
\tab \tab }{\f20\fs20 but doesn't match\tab }{\f22\fs20 \'c9}{\f22\fs20\ul ab}{\f22\fs20 \'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f22\fs20 \par 
}{\f20\fs20 \tab }{\f22\fs20 [abc][xyz]}{\f20\fs20 \tab matches\tab }{\f22\fs20 \'c9ax\'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
\tab \tab }{\f20\fs20 but  doesn't match\tab }{\f22\fs20 \'c9ab\'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
\par 
}{\f20\fs20 \tab }{\f22\fs20 [abc]x}{\f20\fs20 \tab matches\tab }{\f22\fs20 \'c9bx\'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
\tab \tab }{\f20\fs20 but doesn't match}{\f22\fs20 \tab \'c9Bx}{\b\f22\fs20 \'c9}{\f20\fs20 \par 
\par 
\par 
\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 5a.\tab  The pattern}{\f22\fs20  [^}{\f20\fs20 \'a7}{\f22\fs20 ] }{\f20\fs20 matches any character that is not in the string \'a7
.  Special characters will be taken literally in this context.  Again, case sensitivity doesn't apply to characters between square brackets.\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
\tab }{\b\f20\fs20 Examples:\par 
}{\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \tab }{\f20\fs20\ul Pattern}{\f20\fs20 \tab \tab \tab \tab }{\f20\fs20\ul Text\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \tab }{\f22\fs20 [^abc]}{\f20\fs20 \tab matches\tab }{\f22\fs20 \'c9x\'c9\par 
\tab \tab \tab }{\f20\fs20 and matches\tab }{\f22\fs20 \'c9A\'c9\par 
\tab \tab }{\f20\fs20 but doesn't match\tab }{\f22\fs20 \'c9a\'c9\par 
\tab \par 
}{\f20\fs20 \tab }{\f22\fs20 [^abc]a}{\f20\fs20 \tab matches\tab }{\f22\fs20 \'c9xa\'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
\tab \tab }{\f20\fs20 but doesn't match\tab }{\f22\fs20 \'c9aa\'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
}{\f20\fs20 \par 
\tab }{\f22\fs20 [^.]a}{\f20\fs20 \tab \tab matches\tab }{\f22\fs20 \'c9xa\'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
\tab \tab }{\f20\fs20 but doesn't match\tab }{\f22\fs20 \'c9.a\'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
}{\f20\fs20 \par 
\par 
\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 5b.\tab If a string of three characters in the form}{\f22\fs20  [}{\i\f22\fs20 a}{\f22\fs20 -}{\i\f22\fs20 b}{\f22\fs20 ] }{\f20\fs20 
occurs in in the pattern }{\i\f20\fs20 p}{\f20\fs20 , this represents all of the characters from }{\i\f22\fs20 a}{\f20\fs20  to }{\i\f22\fs20 b}{\f20\fs20  inclusive.  All special characters are taken literally;  i.e.,}{\f22\fs20  [!-.] }{\f20\fs20 
denotes the characters from }{\f22\fs20 ! to .}{\f20\fs20 .  Notice that the only way to include the character}{\f22\fs20  ] }{\f20\fs20 in }{\i\f20\fs20 p}{\f20\fs20 
 is to make it the very first character.  Likewise, the only way to include the character}{\f22\fs20  - }{\f20\fs20 in }{\i\f20\fs20 p}{\f20\fs20  is to have it either at the very beginning or the very end of }{\i\f20\fs20 p}{\f20\fs20 
.  Single characters and ranges may both be used between brackets.\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
\tab }{\b\f20\fs20 Examples:\par 
}{\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \tab }{\f20\fs20\ul Pattern}{\f20\fs20 \tab \tab \tab \tab }{\f20\fs20\ul Text}{\f20\fs20 \par 
\tab }{\f22\fs20 [a-c]}{\f20\fs20 \tab \tab matches\tab }{\f22\fs20 \'c9}{\f22\fs20\ul a}{\f22\fs20 c\'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
}{\f20\fs20 \tab \tab \tab and matches\tab }{\f22\fs20 \'c9x}{\f22\fs20\ul c}{\f22\fs20 \'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f22\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \tab }{\f22\fs20 [1x-z]a}{\f20\fs20 \tab matches\tab }{\f22\fs20 \'c9}{\f22\fs20\ul 1a}{\f22\fs20 \'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
\tab \tab \tab }{\f20\fs20 and matches\tab }{\f22\fs20 \'c9}{\f22\fs20\ul x}{\f22\fs20 a\'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \tab }{\f22\fs20 [-x-z]a}{\f20\fs20 \tab matches\tab }{\f22\fs20 \'c9}{\f22\fs20\ul -a}{\f22\fs20 \'c9\par 
\tab \tab \tab }{\f20\fs20 and matches\tab \'c9}{\f20\fs20\ul xa}{\f20\fs20 \'c9}{\f22\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 6.\tab Any pattern }{\i\f20\fs20 p}{\f20\fs20  formed by any combination of rules 1 or 3-5b followed by a}{\f22\fs20  * }{\f20\fs20 forms the pattern}{
\f22\fs20  }{\i\f20\fs20 p}{\f22\fs20 * }{\f20\fs20 that matches zero or more consecutive occurrences of characters matched by}{\f22\fs20  }{\i\f20\fs20 p}{\f20\fs20 .\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
}{\b\f20\fs20 \tab Examples:\par 
}{\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \tab }{\f20\fs20\ul Pattern}{\f20\fs20 \tab \tab \tab \tab }{\f20\fs20\ul Text}{\f20\fs20 \tab }{\f20\fs20\ul With case sensitivity}{\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \tab }{\f22\fs20 [a-c]*}{\f20\fs20 \tab matches\tab }{\f22\fs20 \'c9a\par 
\tab \tab \tab }{\f20\fs20 and matches\tab }{\f22\fs20 \'c9acbca\par 
\tab \tab \tab }{\f20\fs20 and matches\tab nothing\par 
}{\f22\fs20 \par 
}{\f20\fs20 \tab }{\f22\fs20 A[a-z]*}{\f20\fs20 \tab matches\tab }{\f22\fs20 \'c9A\'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
\tab \tab \tab }{\f20\fs20 and matches\tab }{\f22\fs20 \'c9Abcb\'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
\tab \tab \tab }{\f20\fs20 and matches\tab }{\f22\fs20 \'c9abc\'c9}{\f20\fs20 \tab on}{\f22\fs20 \par 
\tab \tab }{\f20\fs20 but doesn't match\tab }{\f22\fs20 \'c9abc\'c9}{\f20\fs20 \tab off}{\f22\fs20 \par 
\par 
}{\f20\fs20 \tab }{\f22\fs20 .*}{\f20\fs20 \tab \tab matches\tab anything from\par 
\tab \tab \tab \tab \tab beginning of a \par 
\tab \tab \tab \tab \tab line to the end of\par 
\tab \tab \tab \tab \tab  the line\par 
}{\f22\fs20 \par 
}{\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \tab }{\f22\fs20 [abc]*}{\f20\fs20 \tab matches\tab }{\f22\fs20 \'c9}{\f22\fs20\ul b}{\f22\fs20 \par 
\tab \tab \tab }{\f20\fs20 and matches\tab }{\f22\fs20 \'c9}{\f22\fs20\ul ab}{\f22\fs20 \par 
\tab \tab }{\f20\fs20 but doesn't match just\tab }{\f22\fs20 \'c9}{\f22\fs20\ul a}{\f22\fs20 b\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f22\fs20 \tab \tab }{\f20\fs20 (because it matches\par 
\tab \tab the longest string\par 
\tab \tab possible)}{\f22\fs20 \par 
\par 
}{\f20\fs20 \tab (}{\f22\fs20 .*)}{\f20\fs20 \tab \tab matches\tab }{\f22\fs20 \'c9(aaa)\'c9}{\f20\fs20 \tab \par 
}{\f22\fs20 \tab \tab \tab }{\f20\fs20 and matches\tab }{\f22\fs20 \'c9()\'c9}{\f20\fs20 \tab \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 \page {\b\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\b\f20\fs20 A closer example:\par 
\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 Let us examine more closely how the pattern}{\f22\fs20  (.*) }{\f20\fs20 
matches text.  This pattern will match any string that is enclosed in parentheses.  This includes the string}{\f22\fs20  ()}{\f20\fs20 , since the sub-pattern}{\f22\fs20  .* }{\f20\fs20 will match the empty string between the}{\f22\fs20  ( }{\f20\fs20 
and the}{\f22\fs20  )}{\f20\fs20 .  But what about the string}{\f22\fs20  (())}{\f20\fs20 ?  Since the pattern}{\f22\fs20  .* }{\f20\fs20 will match any number of occurrences of all characters, won't it match the}{\f22\fs20  (() }{\f20\fs20 
and cause the last}{\f22\fs20  ) }{\f20\fs20 in the string to fail to match?  Or conversely, won't the sub-pattern}{\f22\fs20  (.* }{\f20\fs20 match the whole string, leaving the}{\f22\fs20  ) }{\f20\fs20 at the end of the pattern unmatched?\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 The answer to this is that any pattern of the form}{\f22\fs20  }{\i\f20\fs20 p}{\f22\fs20 * }{\f20\fs20 in a pattern}{\f22\fs20  }{\i\f20\fs20 p}{
\f22\fs20 *}{\i\f20\fs20 y}{\f22\fs20  }{\f20\fs20 will match the }{\f20\fs20\ul largest}{\f20\fs20  number of occurrences of whatever }{\i\f20\fs20 p}{\f20\fs20  matches }{\f20\fs20\ul that still allows a match to }{\i\f20\fs20\ul y}{\f20\fs20 
.  Therefore, in matching}{\f22\fs20  (()) }{\f20\fs20 against the pattern}{\f22\fs20  (.*), }{\f20\fs20 only the inner parentheses in the string}{\f22\fs20  (()) }{\f20\fs20 will be matched by the sub-pattern}{\f22\fs20  .*}{\f20\fs20 .\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
\par 
\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\b\f21 Remembering sub-strings\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 
We now have the ability to form patterns that are composed of sub-patterns, and will find it useful to "remember" sub-strings matched by sub-patterns and to be able to match against those substrings.\par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 \par 
}\pard \li180\ri-20\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx7200\tx9619 {\f20\fs20 7.\tab A pattern surrounded by}{\f22\fs20  \\( }{\f20\fs20 and}{\f22\fs20  \\) }{\f20\fs20 
is a pattern that matches whatever the sub-pattern matches.  This is useful for matching two or more instances of the same string and when doing replacements.\par 
\par 
\tab }{\b\f20\fs20 Example:\par 
}{\f20\fs20 \par 
\tab }{\f20\fs20\ul Pattern}{\f20\fs20 \tab \tab \tab \tab }{\f20\fs20\ul Text}{\f20\fs20 \par 
\tab }{\f22\fs20 \\(abc\\)}{\f20\fs20 \tab matches\tab }{\f22\fs20 \'c9abc}{\f20\fs20 \par 
\tab }{\f22\fs20 \\(ab(\\)}{\f20\fs20 \tab matches\tab }{\f22\fs20 \'c9ab(\par 
\par 
\par 
}{\f20\fs20 \par 
8.\tab A}{\f22\fs20  \\ }{\f20\fs20 followed by }{\i\f20\fs20 n}{\f20\fs20 , where }{\i\f20\fs20 n}{\f20\fs20  is one of the digits }{\f22\fs20 1-9}{\f20\fs20 
, is a pattern that matches whatever was matched by the sub-pattern beginning with the "nth" occurrence of }{\f22\fs20 \\(}{\f20\fs20 .  A pattern}{\f22\fs20  \\}{\i\f20\fs20 n}{\f20\fs20  may be followed by an}{\f22\fs20  *}{\f20\fs20 
, and forms a pattern}{\f22\fs20  }{\f20\fs20 \\}{\i\f20\fs20 n}{\f22\fs20 * }{\f20\fs20 that matches zero or more occurrences of whatever }{\f22\fs20 \\}{\i\f20\fs20 n}{\f20\fs20  matches.\par 
\par 
\tab }{\b\f20\fs20 Examples:\par 
}{\f20\fs20 \par 
\tab }{\f20\fs20\ul Pattern}{\f20\fs20 \tab \tab \tab \tab }{\f20\fs20\ul Text}{\f20\fs20 \par 
\tab }{\f22\fs20 \\(abc\\)\\1}{\f20\fs20 \tab matches\tab }{\f22\fs20 \'c9abcabc\'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
\par 
}{\f20\fs20 \tab }{\f22\fs20 \\(a.c\\)\\1}{\f20\fs20 \tab matches\tab }{\f22\fs20 \'c9axcaxc\'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
}{\f20\fs20 \tab \tab \tab but not\tab \tab }{\f22\fs20 \'c9axcazc\'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
}{\f20\fs20 \tab \tab \tab nor\tab \tab }{\f22\fs20 \'c9axcaXc\'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
}{\b\f20\fs20 \par 
}{\f20\fs20 Note that in this last pattern, the sub-pattern}{\f22\fs20  \\1 }{\f20\fs20 does not imply a re-application of the sub-pattern}{\f22\fs20  a.c}{\f20\fs20 , but what}{\f22\fs20  a.c }{\f20\fs20 matches.  If}{\f22\fs20  \\(a.c\\) }{\f20\fs20 
was matched with the string}{\f22\fs20  axc}{\f20\fs20 , then the sub-pattern}{\f22\fs20  \\1 }{\f20\fs20 would try to match the literal string}{\f22\fs20  axc }{\f20\fs20 against the remainder of the search string.  Therefore, the pattern}{\f22\fs20  \\
(a.c\\)\\1 }{\f20\fs20 will match}{\f22\fs20  axcaxc}{\f20\fs20 , but will not match}{\f22\fs20  axcazc}{\f20\fs20 .\par 
\par 
\par 
\page \par 
}{\b\f21 Constraining matches}{\b\f20 \par 
}{\b\f20\fs20 \par 
}{\f20\fs20 Sometimes it is useful to be able to "constrain" patterns to match only if certain conditions in the context outside the string matched are met.\par 
\par 
9.\tab A pattern surrounded by}{\f22\fs20  \\< }{\f20\fs20 and}{\f22\fs20  \\> }{\f20\fs20 is a pattern that matches whatever is matched by the sub-pattern, provided that the first and last characters of the matched string can be matched by}{\f22\fs20 
 [A-Za-z0-9_] }{\f20\fs20 and that the characters immediately surrounding the matched string cannot be matched by}{\f22\fs20  [A-Za-z0-9_] }{\f20\fs20 (i.e., can be matched by}{\f22\fs20  [^A-Za-z0-9_]}{\f20\fs20 ).\par 
\par 
\tab This is used to match any string that matches the sub-pattern only if the matched string begins and ends on a "word" boundary (a "word" being a C identifier).\par 
\par 
\tab }{\b\f20\fs20 Examples:\par 
}{\f20\fs20 \par 
\tab }{\f20\fs20\ul Pattern}{\f20\fs20 \tab \tab \tab \tab }{\f20\fs20\ul Text}{\f20\fs20 \par 
\tab }{\f22\fs20 \\<ab*\\>}{\f20\fs20 \tab matches\tab }{\f22\fs20 \'c9+}{\f22\fs20\ul ab}{\f22\fs20 +\'c9}{\f20\fs20 \tab }{\f22\fs20 \par 
}{\f20\fs20 \tab \tab but doesn't match\tab }{\f22\fs20 \'c9}{\f22\fs20\ul +ab+}{\f22\fs20 \'c9\par 
\tab }{\f20\fs20 \tab and doesn't match}{\f22\fs20 \tab \'c9+}{\f22\fs20\ul ab}{\f22\fs20 c+}{\f20\fs20 \par 
}{\f22\fs20 \par 
}{\f20\fs20 \par 
\par 
10.\tab A pattern }{\i\f20\fs20 p}{\f20\fs20  that is preceded by a}{\f22\fs20  ^ }{\f20\fs20 forms a pattern}{\f22\fs20  ^}{\i\f22\fs20 p}{\f20\fs20 .  If the pattern}{\f22\fs20  ^}{\i\f22\fs20 p}{\f20\fs20 
 is not preceded by any other pattern, it matches whatever }{\i\f20\fs20 p}{\f20\fs20  matches as long as the first character matched by }{\i\f20\fs20 p}{\f20\fs20  occurs at the beginning of a line.  If the pattern}{\f22\fs20  ^}{\i\f22\fs20 p}{
\f20\fs20  is preceded by another pattern, then the}{\f22\fs20  ^ }{\f20\fs20 is taken literally.\par 
\par 
\tab }{\b\f20\fs20 Examples:\par 
}{\f20\fs20 \par 
\tab }{\f20\fs20\ul Pattern}{\f20\fs20 \tab \tab \tab \tab }{\f20\fs20\ul Text}{\f20\fs20 \par 
\tab }{\f22\fs20 ^ab*}{\f20\fs20 \tab matches\tab \tab }{\f22\fs20\ul ab}{\f22\fs20 \'c9}{\f20\fs20 \tab \par 
\tab \tab but doesn't match\tab }{\f22\fs20 x}{\f22\fs20\ul ab}{\f22\fs20 \'c9}{\f20\fs20 \tab \par 
\par 
}{\f22\fs20 \tab ab^ab*}{\f20\fs20 \tab matches\tab }{\f22\fs20 ab^ab\'c9}{\f20\fs20 \tab \par 
\par 
\par 
\par 
11.\tab A pattern }{\i\f20\fs20 p}{\f20\fs20  that is followed by a}{\f22\fs20  $ }{\f20\fs20 forms a pattern }{\i\f20\fs20 p}{\f22\fs20 $}{\f20\fs20 .  If the pattern }{\i\f20\fs20 p}{\f22\fs20 $}{\f20\fs20 
 is not followed by any other pattern, it matches whatever }{\i\f20\fs20 p}{\f20\fs20  matches as long as the last character matched by }{\i\f20\fs20 p}{\f20\fs20  occurs at the end of a line.  If the pattern }{\i\f20\fs20 p}{\f22\fs20 $ }{\f20\fs20 
is followed by another pattern, then the }{\f22\fs20 $}{\f20\fs20  is taken literally.\par 
\par 
\tab }{\b\f20\fs20 Examples:\par 
}{\f20\fs20 \par 
\tab }{\f20\fs20\ul Pattern}{\f20\fs20 \tab \tab \tab \tab }{\f20\fs20\ul Text}{\f20\fs20 \par 
\tab }{\f22\fs20 ab$}{\f20\fs20 \tab matches\tab \tab }{\f22\fs20 \'c9ab}{\f20\fs20 \tab }{\f22\fs20 \par 
}{\f20\fs20 \tab \tab but doesn't match\tab }{\f22\fs20 \'c9}{\f22\fs20\ul ab}{\f22\fs20 x}{\f20\fs20 \tab }{\f22\fs20 \par 
\par 
}{\f20\fs20 \tab }{\f22\fs20 ab$ab}{\f20\fs20 \tab matches\tab \tab }{\f22\fs20 \'c9ab$ab\'c9\par 
}{\f20\fs20 \par 
\tab }{\f22\fs20 ^ab$}{\f20\fs20 \tab matches\tab \tab }{\f22\fs20 ab}{\f20\fs20 \par 
\tab \tab but doesn't match\tab }{\f22\fs20 ab\'c9\par 
\par 
\par 
}{\f20\fs20 \tab Note that the characters }{\f22\fs20 ^}{\f20\fs20  and }{\f22\fs20 $}{\f20\fs20  constrain pattern matches to begin or end at line boundaries, and so can be combined to constr
ain a pattern to match an entire line only (as in the above example).\par 
\par 
We mentioned at the beginning the ability to search for any identifier beginning with the letter }{\f22\fs20 P}{\f20\fs20 .  This would be accomplished with the pattern }{\f22\fs20 \\<[Pp][A-Za-z0-9_]*\\>}{\f20\fs20 
.  Note that, if you have case sensitivity is off, then the patterns}{\f22\fs20  \\<P[A\endash Za\endash z0\endash 9_]*\\> }{\f20\fs20 and }{\f22\fs20 \\<p[A-Za-z0-9_]*\\>}{\f20\fs20 
 would match the same strings.  Also, if word-match is on, then any of these patterns with the}{\f22\fs20  \\<}{\f20\fs20  and}{\f22\fs20  \\>}{\f20\fs20  removed will match the same strings.\par 
\par 
\par 
\par 
}{\b\f21 Replacement\par 
}{\f20\fs20 \par 
Grep provides not only a more sophisticated method of searching, but a sophisticated method of replacing as well.  In a replacement string, the following substitutions are made before any text replacement occurs:\par 
\par 
1.\tab Each occurrence of the character }{\f22\fs20 &}{\f20\fs20  is replaced with whatever was last matched by the pattern.\par 
\par 
}{\b\f20\fs20 \tab Examples:\par 
}{\f20\fs20 \par 
\tab }{\f20\fs20\ul "Find" string}{\f20\fs20 \tab }{\f20\fs20\ul "Replace" string}{\f20\fs20 \tab }{\f20\fs20\ul Original text}{\f20\fs20 \tab }{\f20\fs20\ul Result}{\f20\fs20 \par 
}{\f22\fs20 \tab abc}{\f20\fs20 \tab \tab }{\f22\fs20 +&}{\f20\fs20 \tab \tab }{\f22\fs20 \'c9abc\'c9}{\f20\fs20 \tab \tab }{\f22\fs20 \'c9+abc\'c9}{\f20\fs20 \par 
}{\f22\fs20 \tab abc}{\f20\fs20 \tab \tab }{\f22\fs20 &&}{\f20\fs20 \tab \tab }{\f22\fs20 \'c9abc\'c9}{\f20\fs20 \tab \tab }{\f22\fs20 \'c9abcabc\'c9}{\f20\fs20 \par 
\tab \par 
\par 
2.\tab Each occurrence of a string of the form }{\f22\fs20 \\}{\i\f20\fs20 n}{\f20\fs20 , where }{\i\f20\fs20 n}{\f20\fs20  is one of the digits }{\f22\fs20 1}{\f20\fs20 -}{\f22\fs20 9}{\f20\fs20 , is repla
ced by whatever was last matched by the sub-pattern beginning with the }{\i\f22\fs20 n}{\f20\fs20 th occurrence of }{\f22\fs20 \\(}{\f20\fs20 .\par 
\par 
}{\b\f20\fs20 \tab Examples:\par 
}{\f20\fs20 \par 
\tab }{\f20\fs20\ul "Find" string}{\f20\fs20 \tab \tab }{\f20\fs20\ul "Replace" string}{\f20\fs20 \tab }{\f20\fs20\ul Original text}{\f20\fs20 \tab }{\f20\fs20\ul Result}{\f20\fs20 \par 
}{\f22\fs20 \tab \\(a*\\)\\(b*\\)}{\f20\fs20 \tab }{\f22\fs20 \\1\\2}{\f20\fs20 \tab \tab }{\f22\fs20 aabb\'c9}{\f20\fs20 \tab \tab }{\f22\fs20 aabb\'c9\par 
\tab \par 
\tab \\(a*\\)\\(b*\\)}{\f20\fs20 \tab }{\f22\fs20 \\2\\1}{\f20\fs20 \tab \tab }{\f22\fs20 aabb\'c9}{\f20\fs20 \tab \tab }{\f22\fs20 bbaa\'c9}{\f20\fs20 \par 
\tab \par 
\par 
\par 
\par 
3.\tab Each occurrence of a string of the form }{\f22\fs20 \\}{\i\f22\fs20 p}{\f20\fs20 , where }{\i\f22\fs20 p}{\f20\fs20  is other than one of the digits 1-9, is replaced by }{\i\f22\fs20 p}{\f20\fs20 .\par 
\par 
}{\b\f20\fs20 \tab Examples:\par 
}{\f20\fs20 \par 
\tab }{\f20\fs20\ul "Find" string}{\f20\fs20 \tab \tab }{\f20\fs20\ul "Replace" string}{\f20\fs20 \tab }{\f20\fs20\ul Original text}{\f20\fs20 \tab }{\f20\fs20\ul Result}{\f20\fs20 \par 
}{\f22\fs20 \tab \\(a*\\)\\(b*\\)}{\f20\fs20 \tab }{\f22\fs20 \\1&\\2\\}{\f20\fs20 \tab }{\f22\fs20 aabb\'c9}{\f20\fs20 \tab \tab }{\f22\fs20 aa&bb\'c9\par 
\tab \par 
\tab \\(a*\\)\\(b*\\)}{\f20\fs20 \tab }{\f22\fs20 \\\\\\2\\1\\\\}{\f20\fs20 \tab }{\f22\fs20 aabb\'c9}{\f20\fs20 \tab \tab }{\f22\fs20 \\bbaa\\\'c9}{\f20\fs20 \par 
\par 
\par 
\tab This allows you to not only be able to search for a string satisfying a complex set of conditions, but also to be able to do a subsequent replacement that varies depending on the string that is matched.\par 
\par 
\par 
\page \par 
}{\b\f21\fs28 Some Examples\par 
}{\b\f20\fs20 \par 
}{\f20\fs20 \bullet \tab 
Suppose that you have written a program that is to become a Macintosh application (i.e., it uses the Macintosh ToolBox instead of stdio for the user interface).  Suppose also that you have discovered that you have forgotten to put a }{\f22\fs20 \\p}{
\f20\fs20  at the beginning of your string constants, so that your program is trying to
 pass C strings instead of Pascal strings to the ToolBox (which only knows how to deal with Pascal strings).  You can easily change all your C strings to Pascal strings by specifying }{\f22\fs20 "\\(.*\\)" }{\f20\fs20 as the search pattern and }{
\f22\fs20 "\\\\p\\1" }{\f20\fs20 as the replacement string.\par 
\par 
\bullet \tab Suppose you decided to reverse the two arguments of the function "foo".  You might try the pattern }{\f22\fs20 foo(\\([^,]*\\),\\([^)]*\\))}{\f20\fs20  as the search pattern and }{\f22\fs20 foo(\\2, \\1)}{\f20\fs20 
 as the replacement pattern.  How does the search pattern work?\par 
\par 
\tab Let's assume we're trying to match some text that looks like }{\f22\fs20 foo(1,*bar)\par 
}{\f20\fs20 \par 
\bullet \tab }{\f22\fs20\ul foo(}{\f22\fs20 \\([^,]*\\),\\([^)]*\\))\tab }{\f20\fs20 matches}{\f22\fs20 \tab }{\f22\fs20\ul foo(}{\f22\fs20 1,*bar)\par 
}{\f20\fs20 \bullet \tab }{\f22\fs20 foo(}{\f22\fs20\ul \\([^,]*\\)}{\f22\fs20 ,\\([^)]*\\))\tab }{\f20\fs20 matches}{\f22\fs20 \tab foo(}{\f22\fs20\ul 1}{\f22\fs20 ,*bar)\par 
}{\f20\fs20 \bullet \tab }{\f22\fs20 foo(\\([^,]*\\)}{\f22\fs20\ul ,}{\f22\fs20 \\([^)]*\\))\tab }{\f20\fs20 matches}{\f22\fs20 \tab foo(1}{\f22\fs20\ul ,}{\f22\fs20 *bar)\par 
}{\f20\fs20 \bullet \tab }{\f22\fs20 foo(\\([^,]*\\),}{\f22\fs20\ul \\([^)]*\\)}{\f22\fs20 )\tab }{\f20\fs20 matches}{\f22\fs20 \tab foo(1,}{\f22\fs20\ul *bar}{\f22\fs20 )\par 
}{\f20\fs20 \bullet \tab }{\f22\fs20 foo(\\([^,]*\\),\\([^)]*\\)}{\f22\fs20\ul )}{\f22\fs20 \tab }{\f20\fs20 matches}{\f22\fs20 \tab foo(1,*bar}{\f22\fs20\ul )}{\f22\fs20 \par 
}{\f20\fs20 \par 
\tab Since }{\f22\fs20 \\([^,]*\\) }{\f20\fs20 matched }{\f22\fs20 1}{\f20\fs20  and }{\f22\fs20 \\([^)]*\\) }{\f20\fs20 matched}{\f22\fs20  *bar}{\f20\fs20 , the two arguments to foo, the replacement pattern }{\f22\fs20 foo(\\2, \\1)}{\f20\fs20 
 will result in }{\f22\fs20 foo(*bar,\~1)}{\f20\fs20 \par 
\par 
\tab This, unfortunately, won't work in the case of }{\f22\fs20 foo(1,(*bar)+2)}{\f20\fs20 , since }{\f22\fs20 \\([^)]*\\)}{\f20\fs20  will match only up to the first right parenthesis, leaving }{\f22\fs20 +2)}{\f20\fs20 
 unmatched.  If we're sure that all calls to foo end with a semi-colon, however, we can change our pattern to }{\f22\fs20 foo(\\([^,]*\\),\\([^;]*\\));  .  }{\f20\fs20 
In this pattern, instead of trying to match the second argument by matching everything up to the first right parenthesis, we match everything up to the}{\f22\fs20  );}{\f20\fs20  which terminates the invocation of foo.\par 
\par 
In this example we showed how to analyze a grep pattern by examining sub-patterns.  This is a good way of figuring out
 how to build a pattern as well.  grep can be thought of as a small and rather cryptic programming language, with each pattern a program and sub-pattern a statement in this language.  If you try to create a grep pattern by testing a small sub-pattern, then
 adding and testing additional sub-patterns until the complete pattern is built, you may find building complex grep patterns not nearly as daunting as you first thought.\par 
\par 
\par 
}}