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 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- This document was generated using DocBuilder 3.3.3 -->
<HTML>
<HEAD>
<TITLE>regexp</TITLE>
<SCRIPT type="text/javascript" src="../../../../doc/erlresolvelinks.js">
</SCRIPT>
<STYLE TYPE="text/css">
<!--
.REFBODY { margin-left: 13mm }
.REFTYPES { margin-left: 8mm }
-->
</STYLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#FF00FF"
ALINK="#FF0000">
<!-- refpage -->
<CENTER>
<A HREF="http://www.erlang.se">
<IMG BORDER=0 ALT="[Ericsson AB]" SRC="min_head.gif">
</A>
<H1>regexp</H1>
</CENTER>
<H3>MODULE</H3>
<DIV CLASS=REFBODY>
regexp
</DIV>
<H3>MODULE SUMMARY</H3>
<DIV CLASS=REFBODY>
Regular Expression Functions for Strings
</DIV>
<H3>DESCRIPTION</H3>
<DIV CLASS=REFBODY>
<P>This module contains functions for regular expression
matching and substitution.
</DIV>
<H3>EXPORTS</H3>
<P><A NAME="match/2"><STRONG><CODE>match(String, RegExp) -> MatchRes</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY><P>Types:
<DIV CLASS=REFTYPES>
<P>
<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>
</DIV>
</DIV>
<DIV CLASS=REFBODY>
<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>
</DIV>
<P><A NAME="first_match/2"><STRONG><CODE>first_match(String, RegExp) -> MatchRes</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY><P>Types:
<DIV CLASS=REFTYPES>
<P>
<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>
</DIV>
</DIV>
<DIV CLASS=REFBODY>
<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>
</DIV>
<P><A NAME="matches/2"><STRONG><CODE>matches(String, RegExp) -> MatchRes</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY><P>Types:
<DIV CLASS=REFTYPES>
<P>
<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>
</DIV>
</DIV>
<DIV CLASS=REFBODY>
<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>
</DIV>
<P><A NAME="sub/3"><STRONG><CODE>sub(String, RegExp, New) -> SubRes</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY><P>Types:
<DIV CLASS=REFTYPES>
<P>
<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>
</DIV>
</DIV>
<DIV CLASS=REFBODY>
<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>
</DIV>
<P><A NAME="gsub/3"><STRONG><CODE>gsub(String, RegExp, New) -> SubRes</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY><P>Types:
<DIV CLASS=REFTYPES>
<P>
<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>
</DIV>
</DIV>
<DIV CLASS=REFBODY>
<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>
</DIV>
<P><A NAME="split/2"><STRONG><CODE>split(String, RegExp) -> SplitRes</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY><P>Types:
<DIV CLASS=REFTYPES>
<P>
<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>
</DIV>
</DIV>
<DIV CLASS=REFBODY>
<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>
</DIV>
<P><A NAME="sh_to_awk/1"><STRONG><CODE>sh_to_awk(ShRegExp) -> AwkRegExp</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY><P>Types:
<DIV CLASS=REFTYPES>
<P>
<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>
</DIV>
</DIV>
<DIV CLASS=REFBODY>
<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.
</DIV>
<P><A NAME="parse/1"><STRONG><CODE>parse(RegExp) -> ParseRes</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY><P>Types:
<DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>RegExp = string()</CODE></STRONG><BR>
<STRONG><CODE>ParseRes = {ok,RE} | {error,errordesc()}</CODE></STRONG><BR>
</DIV>
</DIV>
<DIV CLASS=REFBODY>
<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>
</DIV>
<P><A NAME="format_error/1"><STRONG><CODE>format_error(ErrorDescriptor) -> Chars</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY><P>Types:
<DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>ErrorDescriptor = errordesc()</CODE></STRONG><BR>
<STRONG><CODE>Chars = [char() | Chars]</CODE></STRONG><BR>
</DIV>
</DIV>
<DIV CLASS=REFBODY>
<P>Returns a string which describes the error <CODE>ErrorDescriptor</CODE>
returned when there is an error in a regular expression.
</DIV>
<H3>Regular Expressions</H3>
<DIV CLASS=REFBODY>
<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.
</DIV>
<H3>AUTHORS</H3>
<DIV CLASS=REFBODY>
Robert Virding - support@erlang.ericsson.se<BR>
</DIV>
<CENTER>
<HR>
<SMALL>stdlib 1.14.2<BR>
Copyright © 1991-2006
<A HREF="http://www.erlang.se">Ericsson AB</A><BR>
</SMALL>
</CENTER>
</BODY>
</HTML>
|