aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stdlib/doc/src/regexp.xml
blob: 35d8e1c3f84d46f3120daceacf300fac505adc59 (plain) (blame)
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
<?xml version="1.0" encoding="latin1" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">

<erlref>
  <header>
    <copyright>
      <year>1996</year><year>2011</year>
      <holder>Ericsson AB. All Rights Reserved.</holder>
    </copyright>
    <legalnotice>
      The contents of this file are subject to the Erlang Public License,
      Version 1.1, (the "License"); you may not use this file except in
      compliance with the License. You should have received a copy of the
      Erlang Public License along with this software. If not, it can be
      retrieved online at http://www.erlang.org/.
    
      Software distributed under the License is distributed on an "AS IS"
      basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
      the License for the specific language governing rights and limitations
      under the License.
    
    </legalnotice>

    <title>regexp</title>
    <prepared>Robert Virding</prepared>
    <responsible>Bjarne Dacker</responsible>
    <docno>1</docno>
    <approved>Bjarne D&auml;cker</approved>
    <checked></checked>
    <date>96-09-28</date>
    <rev>A</rev>
    <file>regexp.sgml</file>
  </header>
  <module>regexp</module>
  <modulesummary>Regular Expression Functions for Strings</modulesummary>
  <description>
    <note><p>This module has been obsoleted by the
    <seealso marker="re">re</seealso> module and will be removed in a future
    release.</p></note>
    <p>This module contains functions for regular expression
      matching and substitution.</p>
  </description>
  <datatypes>
    <datatype>
      <name name="errordesc"></name>
    </datatype>
    <datatype>
      <name name="regexp"></name>
      <desc><p>Internal representation of a regular expression.</p></desc>
    </datatype>
  </datatypes>
  <funcs>
    <func>
      <name name="match" arity="2"/>
      <fsummary>Match a regular expression</fsummary>
      <desc>
        <p>Finds the first, longest match of the regular expression <c><anno>RegExp</anno></c> in <c><anno>String</anno></c>. This function searches for the longest possible match and returns the first one found if there are several expressions of the same length. It returns as follows:</p>
        <taglist>
          <tag><c>{match,<anno>Start</anno>,<anno>Length</anno>}</c></tag>
          <item>
            <p>if the match succeeded. <c><anno>Start</anno></c> is the starting
              position of the match, and <c><anno>Length</anno></c> is the length of
              the matching string.</p>
          </item>
          <tag><c>nomatch</c></tag>
          <item>
            <p>if there were no matching characters.</p>
          </item>
          <tag><c>{error,<anno>Error</anno>}</c></tag>
          <item>
            <p>if there was an error in <c><anno>RegExp</anno></c>.</p>
          </item>
        </taglist>
      </desc>
    </func>
    <func>
      <name name="first_match" arity="2"/>
      <fsummary>Match a regular expression</fsummary>
      <desc>
        <p>Finds the first match of the regular expression <c><anno>RegExp</anno></c> in <c><anno>String</anno></c>. This call is
          usually faster than <c>match</c> and it is also a useful way to ascertain that a match exists. It returns as follows:</p>
        <taglist>
          <tag><c>{match,<anno>Start</anno>,<anno>Length</anno>}</c></tag>
          <item>
            <p>if the match succeeded. <c><anno>Start</anno></c> is the starting
              position of the match and <c><anno>Length</anno></c> is the length of
              the matching string.</p>
          </item>
          <tag><c>nomatch</c></tag>
          <item>
            <p>if there were no matching characters.</p>
          </item>
          <tag><c>{error,<anno>Error</anno>}</c></tag>
          <item>
            <p>if there was an error in <c><anno>RegExp</anno></c>.</p>
          </item>
        </taglist>
      </desc>
    </func>
    <func>
      <name name="matches" arity="2"/>
      <fsummary>Match a regular expression</fsummary>
      <desc>
        <p>Finds all non-overlapping matches of the
          expression <c><anno>RegExp</anno></c> in <c><anno>String</anno></c>.
          It returns as follows:</p>
        <taglist>
          <tag><c>{match, <anno>Matches</anno>}</c></tag>
          <item>
            <p>if the regular expression was correct.
              The list will be empty if there was no match. Each element in the list looks like <c>{<anno>Start</anno>, <anno>Length</anno>}</c>, where <c><anno>Start</anno></c> is the starting position of the match, and <c><anno>Length</anno></c> is the length of the matching string.</p>
          </item>
          <tag><c>{error,<anno>Error</anno>}</c></tag>
          <item>
            <p>if there was an error in <c><anno>RegExp</anno></c>.</p>
          </item>
        </taglist>
      </desc>
    </func>
    <func>
      <name name="sub" arity="3"/>
      <fsummary>Substitute the first occurrence of a regular expression</fsummary>
      <desc>
        <p>Substitutes the first occurrence of a substring matching <c><anno>RegExp</anno></c> in <c><anno>String</anno></c> with the string <c><anno>New</anno></c>. A <c><![CDATA[&]]></c> in the string <c><anno>New</anno></c> is replaced by the matched substring of <c><anno>String</anno></c>.  <c><![CDATA[\&]]></c> puts a literal <c><![CDATA[&]]></c> into the replacement string. It returns as follows:</p>
        <taglist>
          <tag><c>{ok,<anno>NewString</anno>,<anno>RepCount</anno>}</c></tag>
          <item>
            <p>if <c><anno>RegExp</anno></c> is correct. <c><anno>RepCount</anno></c> is the number of replacements which have been made
              (this will be either 0 or 1).</p>
          </item>
          <tag><c>{error, <anno>Error</anno>}</c></tag>
          <item>
            <p>if there is an error in <c><anno>RegExp</anno></c>.</p>
          </item>
        </taglist>
      </desc>
    </func>
    <func>
      <name name="gsub" arity="3"/>
      <fsummary>Substitute all occurrences of a regular expression</fsummary>
      <desc>
        <p>The same as <c>sub</c>, except that all non-overlapping
          occurrences of a substring matching
          <c><anno>RegExp</anno></c> in <c><anno>String</anno></c> are replaced by the string <c><anno>New</anno></c>. It returns:</p>
        <taglist>
          <tag><c>{ok,<anno>NewString</anno>,<anno>RepCount</anno>}</c></tag>
          <item>
            <p>if <c><anno>RegExp</anno></c> is correct. <c><anno>RepCount</anno></c> is the number of replacements which have been made.</p>
          </item>
          <tag><c>{error, <anno>Error</anno>}</c></tag>
          <item>
            <p>if there is an error in <c><anno>RegExp</anno></c>.</p>
          </item>
        </taglist>
      </desc>
    </func>
    <func>
      <name name="split" arity="2"/>
      <fsummary>Split a string into fields</fsummary>
      <desc>
        <p><c><anno>String</anno></c> is split into fields (sub-strings) by the
          regular expression <c><anno>RegExp</anno></c>.</p>
        <p>If the separator expression is <c>" "</c> (a single space),
          then the fields are separated by blanks and/or tabs and
          leading and trailing blanks and tabs are discarded. For all
          other values of the separator, leading and trailing blanks
          and tabs are not discarded. It returns:</p>
        <taglist>
          <tag><c>{ok, <anno>FieldList</anno>}</c></tag>
          <item>
            <p>to indicate that the string has been split up into the fields of
              <c><anno>FieldList</anno></c>.</p>
          </item>
          <tag><c>{error, <anno>Error</anno>}</c></tag>
          <item>
            <p>if there is an error in <c><anno>RegExp</anno></c>.</p>
          </item>
        </taglist>
      </desc>
    </func>
    <func>
      <name name="sh_to_awk" arity="1"/>
      <fsummary>Convert an <c>sh</c>regular expression into an <c>AWK</c>one</fsummary>
      <desc>
        <p>Converts the <c>sh</c> type regular expression
          <c><anno>ShRegExp</anno></c> into a full <c>AWK</c> regular
          expression. Returns the converted regular expression
          string. <c>sh</c> expressions are used in the shell for
          matching file names and have the following special
          characters:</p>
        <taglist>
          <tag><c>*</c></tag>
          <item>
            <p>matches any string including the null string.</p>
          </item>
          <tag><c>?</c></tag>
          <item>
            <p>matches any single character.</p>
          </item>
          <tag><c>[...]</c></tag>
          <item>
            <p>matches any of the enclosed characters. Character
              ranges are specified by a pair of characters separated
              by a <c>-</c>. If the first character after <c>[</c> is a
              <c>!</c>, then any character not enclosed is matched.</p>
          </item>
        </taglist>
        <p>It may sometimes be more practical to use <c>sh</c> type
          expansions as they are simpler and easier to use, even though they are not as powerful.</p>
      </desc>
    </func>
    <func>
      <name name="parse" arity="1"/>
      <fsummary>Parse a regular expression</fsummary>
      <desc>
        <p>Parses the regular expression <c><anno>RegExp</anno></c> and builds the
          internal representation used in the other regular expression
          functions. Such representations can be used in all of the
          other functions instead of a regular expression string. This
          is more efficient when the same regular expression is used
          in many strings. It returns:</p>
        <taglist>
          <tag><c>{ok, <anno>RE</anno>}</c></tag>
          <item>
            <p>if <c>RegExp</c> is correct and <c><anno>RE</anno></c> is the internal representation.</p>
          </item>
          <tag><c>{error, <anno>Error</anno>}</c></tag>
          <item>
            <p>if there is an error in <c><anno>RegExp</anno></c>.</p>
          </item>
        </taglist>
      </desc>
    </func>
    <func>
      <name name="format_error" arity="1"/>
      <fsummary>Format an error descriptor</fsummary>
      <desc>
        <p>Returns a string which describes the error <c><anno>ErrorDescriptor</anno></c>
          returned when there is an error in a regular expression.</p>
      </desc>
    </func>
  </funcs>

  <section>
    <title>Regular Expressions</title>
    <p>The regular expressions allowed here is a subset of the set found
      in <c>egrep</c> and in the <c>AWK</c> programming language, as
      defined in the book, <c>The AWK Programming Language, by A. V. Aho, B. W. Kernighan, P. J. Weinberger</c>. They are
      composed of the following characters:</p>
    <taglist>
      <tag>c</tag>
      <item>
        <p>matches the non-metacharacter <c>c</c>.</p>
      </item>
      <tag>\c</tag>
      <item>
        <p>matches the escape sequence or literal character <c>c</c>.</p>
      </item>
      <tag>.</tag>
      <item>
        <p>matches any character.</p>
      </item>
      <tag>^</tag>
      <item>
        <p>matches the beginning of a string.</p>
      </item>
      <tag>$</tag>
      <item>
        <p>matches the end of a string.</p>
      </item>
      <tag>[abc...]</tag>
      <item>
        <p>character class, which matches any of the characters
          <c>abc...</c> Character ranges are specified by a pair of
          characters separated by a <c>-</c>.</p>
      </item>
      <tag>[^abc...]</tag>
      <item>
        <p>negated character class, which matches any character except
          <c>abc...</c>.</p>
      </item>
      <tag>r1 | r2</tag>
      <item>
        <p>alternation. It matches either <c>r1</c> or <c>r2</c>.</p>
      </item>
      <tag>r1r2</tag>
      <item>
        <p>concatenation. It matches <c>r1</c> and then <c>r2</c>.</p>
      </item>
      <tag>r+</tag>
      <item>
        <p>matches one or more <c>r</c>s.</p>
      </item>
      <tag>r*</tag>
      <item>
        <p>matches zero or more <c>r</c>s.</p>
      </item>
      <tag>r?</tag>
      <item>
        <p>matches zero or one <c>r</c>s.</p>
      </item>
      <tag>(r)</tag>
      <item>
        <p>grouping. It matches <c>r</c>.</p>
      </item>
    </taglist>
    <p>The escape sequences allowed are the same as for Erlang
      strings:</p>
    <taglist>
      <tag><c>\b</c></tag>
      <item>
        <p>backspace</p>
      </item>
      <tag><c>\f</c></tag>
      <item>
        <p>form feed </p>
      </item>
      <tag><c>\n</c></tag>
      <item>
        <p>newline (line feed) </p>
      </item>
      <tag><c>\r</c></tag>
      <item>
        <p>carriage return </p>
      </item>
      <tag><c>\t</c></tag>
      <item>
        <p>tab </p>
      </item>
      <tag><c>\e</c></tag>
      <item>
        <p>escape </p>
      </item>
      <tag><c>\v</c></tag>
      <item>
        <p>vertical tab </p>
      </item>
      <tag><c>\s</c></tag>
      <item>
        <p>space </p>
      </item>
      <tag><c>\d</c></tag>
      <item>
        <p>delete </p>
      </item>
      <tag><c>\ddd</c></tag>
      <item>
        <p>the octal value ddd </p>
      </item>
      <tag><c>\xhh</c></tag>
      <item>
        <p>The hexadecimal value <c>hh</c>.</p>
      </item>
      <tag><c>\x{h...}</c></tag>
      <item>
        <p>The hexadecimal value <c>h...</c>.</p>
      </item>
      <tag><c>\c</c></tag>
      <item>
        <p>any other character literally, for example <c>\\</c> for backslash,
          <c>\"</c> for ")</p>
      </item>
    </taglist>
    <p>To make these functions easier to use, in combination with the
      function <c>io:get_line</c> which terminates the input line with
      a new line, the <c>$</c> characters also matches a string ending
      with <c>"...\n"</c>. The following examples
      define Erlang data types:</p>
    <pre>
Atoms     [a-z][0-9a-zA-Z_]*

Variables [A-Z_][0-9a-zA-Z_]*

Floats    (\+|-)?[0-9]+\.[0-9]+((E|e)(\+|-)?[0-9]+)?</pre>
    <p>Regular expressions are written as Erlang strings when used with the functions in this module. This means that any <c>\</c> or <c>"</c> characters in a regular expression
      string must be written with <c>\</c> as they are also escape characters for the string. For example, the regular expression string for Erlang floats is:
      <c>"(\\+|-)?[0-9]+\\.[0-9]+((E|e)(\\+|-)?[0-9]+)?"</c>.</p>
    <p>It is not really necessary to have the escape sequences as part of the regular expression syntax as they can always be generated directly in the string. They are included for completeness and can they can also be useful when generating regular expressions, or when they are entered other than with Erlang strings.</p>
  </section>
</erlref>