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
|
<HTML>
<HEAD>
<!-- refpage -->
<TITLE>regexp</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<CENTER>
<A HREF="http://www.erlang.se"><IMG BORDER=0 ALT="[Erlang Systems]" SRC="min_head.gif"></A>
<H1>regexp</H1>
</CENTER>
<H3>MODULE</H3>
<UL>
regexp</UL>
<H3>MODULE SUMMARY</H3>
<UL>
Regular Expression Functions for Strings
</UL>
<H3>DESCRIPTION</H3>
<UL>
<P>This module contains functions for regular expression
matching and substitution.
</UL>
<H3>EXPORTS</H3>
<P><A NAME="match%2"><STRONG><CODE>match(String, RegExp) -> MatchRes</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>String = RegExp = string()</CODE></STRONG><BR>
<STRONG><CODE>MatchRes = {match,Start,Length} | nomatch | {error,errordesc()}</CODE></STRONG><BR>
<STRONG><CODE>Start = Length = integer()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Finds the first, longest match of the regular expression <CODE>RegExp</CODE> in <CODE>String</CODE>. 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><DL>
<DT><CODE>{match,Start,Length}</CODE> </DT>
<DD>if the match succeeded. <CODE>Start</CODE> is the starting
position of the match, and <CODE>Length</CODE> is the length of
the matching string.<BR>
</DD>
<DT><CODE>nomatch</CODE> </DT>
<DD>if there were no matching characters.<BR>
</DD>
<DT><CODE>{error,Error}</CODE> </DT>
<DD>if there was an error in <CODE>RegExp</CODE>.<BR>
</DD>
</DL>
</UL>
<P><A NAME="first_match%2"><STRONG><CODE>first_match(String, RegExp) -> MatchRes</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>String = RegExp = string()</CODE></STRONG><BR>
<STRONG><CODE>MatchRes = {match,Start,Length} | nomatch |
{error,errordesc()}</CODE></STRONG><BR>
<STRONG><CODE>Start = Length = integer()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Finds the first match of the regular expression <CODE>RegExp</CODE> in <CODE>String</CODE>. This call is
usually faster than <CODE>match</CODE> and it is also a useful way to ascertain that a match exists. It returns as follows:
<P><DL>
<DT><CODE>{match,Start,Length}</CODE></DT>
<DD> if the match succeeded. <CODE>Start</CODE> is the starting
position of the match and <CODE>Length</CODE> is the length of
the matching string.<BR>
</DD>
<DT><CODE>nomatch</CODE> </DT>
<DD>if there were no matching characters.<BR>
</DD>
<DT><CODE>{error,Error}</CODE></DT>
<DD> if there was an error in <CODE>RegExp</CODE>.<BR>
</DD>
</DL>
</UL>
<P><A NAME="matches%2"><STRONG><CODE>matches(String, RegExp) -> MatchRes</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>String = RegExp = string()</CODE></STRONG><BR>
<STRONG><CODE>MatchRes = {match, Matches} | {error, errordesc()}</CODE></STRONG><BR>
<STRONG><CODE>Matches = list()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Finds all non-overlapping matches of the
expression <CODE>RegExp</CODE> in <CODE>String</CODE>.
It returns as follows:
<P><DL>
<DT><CODE>{match, Matches}</CODE></DT>
<DD> if the regular expression was correct.
The list will be empty if there was no match. Each element in the list looks like <CODE>{Start, Length}</CODE>, where <CODE>Start</CODE> is the starting position of the match, and <CODE>Length</CODE> is the length of the matching string.<BR>
</DD>
<DT><CODE>{error,Error}</CODE> </DT>
<DD>if there was an error in <CODE>RegExp</CODE>.<BR>
</DD>
</DL>
</UL>
<P><A NAME="sub%3"><STRONG><CODE>sub(String, RegExp, New) -> SubRes</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>String = RegExp = New = string()</CODE></STRONG><BR>
<STRONG><CODE>SubRes = {ok,NewString,RepCount} | {error,errordesc()}</CODE></STRONG><BR>
<STRONG><CODE>RepCount = integer()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Substitutes the first occurrence of a substring matching <CODE>RegExp</CODE> in <CODE>String</CODE> with the string <CODE>New</CODE>. A <CODE>&</CODE> in the string <CODE>New</CODE> is replaced by the matched substring of <CODE>String</CODE>. <CODE>\&</CODE> puts a literal <CODE>&</CODE> into the replacement string. It returns as follows:
<P><DL>
<DT><CODE>{ok,NewString,RepCount}</CODE></DT>
<DD> if <CODE>RegExp</CODE> is correct. <CODE>RepCount</CODE> is the number of replacements which have been made
(this will be either 0 or 1).<BR>
</DD>
<DT><CODE>{error, Error}</CODE></DT>
<DD> if there is an error in <CODE>RegExp</CODE>.<BR>
</DD>
</DL>
</UL>
<P><A NAME="gsub%3"><STRONG><CODE>gsub(String, RegExp, New) -> SubRes</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>String = RegExp = New = string()</CODE></STRONG><BR>
<STRONG><CODE>SubRes = {ok,NewString,RepCount} | {error,errordesc()}</CODE></STRONG><BR>
<STRONG><CODE>RepCount = integer()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>The same as <CODE>sub</CODE>, except that all non-overlapping
occurrences of a substring matching
<CODE>RegExp</CODE> in <CODE>String</CODE> are replaced by the string <CODE>New</CODE>. It returns:
<P><DL>
<DT><CODE>{ok,NewString,RepCount}</CODE> </DT>
<DD>if <CODE>RegExp</CODE> is correct. <CODE>RepCount</CODE> is the number of replacements which have been made.<BR>
</DD>
<DT><CODE>{error, Error}</CODE> </DT>
<DD>if there is an error in <CODE>RegExp</CODE>.<BR>
</DD>
</DL>
</UL>
<P><A NAME="split%2"><STRONG><CODE>split(String, RegExp) -> SplitRes</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>String = RegExp = string()</CODE></STRONG><BR>
<STRONG><CODE>SubRes = {ok,FieldList} | {error,errordesc()}</CODE></STRONG><BR>
<STRONG><CODE>Fieldlist = [string()]</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P><CODE>String</CODE> is split into fields (sub-strings) by the
regular expression <CODE>RegExp</CODE>.
<P>If the separator expression is <CODE>" "</CODE> (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><DL>
<DT><CODE>{ok, FieldList}</CODE> </DT>
<DD>to indicate that the string has been split up into the fields of
<CODE>FieldList</CODE>.<BR>
</DD>
<DT><CODE>{error, Error}</CODE></DT>
<DD> if there is an error in <CODE>RegExp</CODE>.<BR>
</DD>
</DL>
</UL>
<P><A NAME="sh_to_awk%1"><STRONG><CODE>sh_to_awk(ShRegExp) -> AwkRegExp</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>ShRegExp AwkRegExp = string()</CODE></STRONG><BR>
<STRONG><CODE>SubRes = {ok,NewString,RepCount} | {error,errordesc()}</CODE></STRONG><BR>
<STRONG><CODE>RepCount = integer()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Converts the <CODE>sh</CODE> type regular expression
<CODE>ShRegExp</CODE> into a full <CODE>AWK</CODE> regular
expression. Returns the converted regular expression
string. <CODE>sh</CODE> expressions are used in the shell for
matching file names and have the following special
characters:
<P><DL>
<DT><CODE>*</CODE></DT>
<DD>matches any string including the null string.
<BR>
</DD>
<DT><CODE>?</CODE></DT>
<DD>matches any single character.
<BR>
</DD>
<DT><CODE>[...]</CODE></DT>
<DD> matches any of the enclosed characters. Character
ranges are specified by a pair of characters separated
by a <CODE>-</CODE>. If the first character after <CODE>[</CODE> is a
<CODE>!</CODE>, then any character not enclosed is matched.
<BR>
</DD>
</DL>
<P>It may sometimes be more practical to use <CODE>sh</CODE> type
expansions as they are simpler and easier to use, even though they are not as powerful.
</UL>
<P><A NAME="parse%1"><STRONG><CODE>parse(RegExp) -> ParseRes</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>RegExp = string()</CODE></STRONG><BR>
<STRONG><CODE>ParseRes = {ok,RE} | {error,errordesc()}</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Parses the regular expression <CODE>RegExp</CODE> 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><DL>
<DT><CODE>{ok, RE}</CODE> if <CODE>RegExp</CODE> is correct and <CODE>RE</CODE> is the internal
representation.</DT>
<DD><BR>
</DD>
<DT><CODE>{error, Error}</CODE> if there is an error in <CODE>RegExpString</CODE>.</DT>
<DD><BR>
</DD>
</DL>
</UL>
<P><A NAME="format_error%1"><STRONG><CODE>format_error(ErrorDescriptor) -> string()</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>ErrorDescriptor = errordesc()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Returns a string which describes the error <CODE>ErrorDescriptor</CODE>
returned when there is an error in a regular expression.
</UL>
<H3>Regular Expressions</H3>
<UL>
<P>The regular expressions allowed here is a subset of the set found
in <CODE>egrep</CODE> and in the <CODE>AWK</CODE> programming language, as
defined in the book, <CODE>The AWK Programming Language, by
A. V. Aho, B. W. Kernighan, P. J. Weinberger</CODE>. They are
composed of the following characters:
<P><DL>
<DT>c</DT>
<DD>matches the non-metacharacter <CODE>c</CODE>.
<BR>
</DD>
<DT>\c</DT>
<DD>matches the escape sequence or literal character <CODE>c</CODE>.
<BR>
</DD>
<DT>.</DT>
<DD>matches any character.
<BR>
</DD>
<DT>^</DT>
<DD>matches the beginning of a string.
<BR>
</DD>
<DT>$</DT>
<DD>matches the end of a string.
<BR>
</DD>
<DT>[abc...]</DT>
<DD>character class, which matches any of the characters
<CODE>abc...</CODE> Character ranges are specified by a pair of
characters separated by a <CODE>-</CODE>.
<BR>
</DD>
<DT>[^abc...]</DT>
<DD>negated character class, which matches any character except
<CODE>abc...</CODE>.
<BR>
</DD>
<DT>r1 | r2</DT>
<DD>alternation. It matches either <CODE>r1</CODE> or <CODE>r2</CODE>.
<BR>
</DD>
<DT>r1r2</DT>
<DD>concatenation. It matches <CODE>r1</CODE> and then <CODE>r2</CODE>.
<BR>
</DD>
<DT>r+</DT>
<DD>matches one or more <CODE>r</CODE>s.
<BR>
</DD>
<DT>r*</DT>
<DD>matches zero or more <CODE>r</CODE>s.
<BR>
</DD>
<DT>r?</DT>
<DD>matches zero or one <CODE>r</CODE>s.
<BR>
</DD>
<DT>(r)</DT>
<DD>grouping. It matches <CODE>r</CODE>.
<BR>
</DD>
</DL>
<P>The escape sequences allowed are the same as for Erlang
strings:
<P><DL>
<DT><CODE>\b</CODE></DT>
<DD>backspace<BR>
</DD>
<DT><CODE>\f</CODE></DT>
<DD>form feed <BR>
</DD>
<DT><CODE>\n</CODE></DT>
<DD>newline (line feed) <BR>
</DD>
<DT><CODE>\r</CODE></DT>
<DD>carriage return <BR>
</DD>
<DT><CODE>\t</CODE></DT>
<DD>tab <BR>
</DD>
<DT><CODE>\e</CODE></DT>
<DD>escape <BR>
</DD>
<DT><CODE>\v</CODE></DT>
<DD>vertical tab <BR>
</DD>
<DT><CODE>\s</CODE></DT>
<DD>space <BR>
</DD>
<DT><CODE>\d</CODE></DT>
<DD>delete <BR>
</DD>
<DT><CODE>\ddd</CODE></DT>
<DD>the octal value ddd <BR>
</DD>
<DT><CODE>\c</CODE></DT>
<DD>any other character literally, for example <CODE>\\</CODE> for backslash,
<CODE>\"</CODE> for ")
<BR>
</DD>
</DL>
<P>To make these functions easier to use, in combination with the
function <CODE>io:get_line</CODE> which terminates the input line with
a new line, the <CODE>$</CODE> characters also matches a string ending
with <CODE>"...\n"</CODE>. The following examples
define Erlang data types:
<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 <CODE>\</CODE> or <CODE>"</CODE> characters in a regular expression
string must be written with <CODE>\</CODE> as they are also escape characters for the string. For example, the regular expression string for Erlang floats is:
<CODE>"(\\+|-)?[0-9]+\\.[0-9]+((E|e)(\\+|-)?[0-9]+)?"</CODE>.
<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.
</UL>
<H3>AUTHORS</H3>
<UL>
Robert Virding - support@erlang.ericsson.se<BR>
</UL>
<CENTER>
<HR>
<FONT SIZE=-1>stdlib 1.10<BR>
Copyright © 1991-2001
<A HREF="http://www.erlang.se">Ericsson Utvecklings AB</A><BR>
<!--#include virtual="/ssi/otp_footer.html"-->
</FONT>
</CENTER>
</BODY>
</HTML>
|