413 |
the string @samp{fo}. Still trivial. To do something nontrivial, you |
the string @samp{fo}. Still trivial. To do something nontrivial, you |
414 |
need to use one of the special characters. Here is a list of them. |
need to use one of the special characters. Here is a list of them. |
415 |
|
|
416 |
@table @kbd |
@table @asis |
417 |
@item .@: @r{(Period)} |
@item @kbd{.}@: @r{(Period)} |
418 |
is a special character that matches any single character except a newline. |
is a special character that matches any single character except a newline. |
419 |
Using concatenation, we can make regular expressions like @samp{a.b}, which |
Using concatenation, we can make regular expressions like @samp{a.b}, which |
420 |
matches any three-character string that begins with @samp{a} and ends with |
matches any three-character string that begins with @samp{a} and ends with |
421 |
@samp{b}.@refill |
@samp{b}.@refill |
422 |
|
|
423 |
@item * |
@item @kbd{*} |
424 |
is not a construct by itself; it is a postfix operator that means to |
is not a construct by itself; it is a postfix operator that means to |
425 |
match the preceding regular expression repetitively as many times as |
match the preceding regular expression repetitively as many times as |
426 |
possible. Thus, @samp{o*} matches any number of @samp{o}s (including no |
possible. Thus, @samp{o*} matches any number of @samp{o}s (including no |
441 |
The next alternative is for @samp{a*} to match only two @samp{a}s. |
The next alternative is for @samp{a*} to match only two @samp{a}s. |
442 |
With this choice, the rest of the regexp matches successfully.@refill |
With this choice, the rest of the regexp matches successfully.@refill |
443 |
|
|
444 |
@item + |
@item @kbd{+} |
445 |
is a postfix operator, similar to @samp{*} except that it must match |
is a postfix operator, similar to @samp{*} except that it must match |
446 |
the preceding expression at least once. So, for example, @samp{ca+r} |
the preceding expression at least once. So, for example, @samp{ca+r} |
447 |
matches the strings @samp{car} and @samp{caaaar} but not the string |
matches the strings @samp{car} and @samp{caaaar} but not the string |
448 |
@samp{cr}, whereas @samp{ca*r} matches all three strings. |
@samp{cr}, whereas @samp{ca*r} matches all three strings. |
449 |
|
|
450 |
@item ? |
@item @kbd{?} |
451 |
is a postfix operator, similar to @samp{*} except that it can match the |
is a postfix operator, similar to @samp{*} except that it can match the |
452 |
preceding expression either once or not at all. For example, |
preceding expression either once or not at all. For example, |
453 |
@samp{ca?r} matches @samp{car} or @samp{cr}; nothing else. |
@samp{ca?r} matches @samp{car} or @samp{cr}; nothing else. |
454 |
|
|
455 |
@item *?, +?, ?? |
@item @kbd{*?}, @kbd{+?}, @kbd{??} |
456 |
@cindex non-greedy regexp matching |
@cindex non-greedy regexp matching |
457 |
are non-greedy variants of the operators above. The normal operators |
are non-greedy variants of the operators above. The normal operators |
458 |
@samp{*}, @samp{+}, @samp{?} are @dfn{greedy} in that they match as |
@samp{*}, @samp{+}, @samp{?} are @dfn{greedy} in that they match as |
473 |
a newline, it matches the whole string. Since it @emph{can} match |
a newline, it matches the whole string. Since it @emph{can} match |
474 |
starting at the first @samp{a}, it does. |
starting at the first @samp{a}, it does. |
475 |
|
|
476 |
@item \@{@var{n}\@} |
@item @kbd{\@{@var{n}\@}} |
477 |
is a postfix operator that specifies repetition @var{n} times---that |
is a postfix operator that specifies repetition @var{n} times---that |
478 |
is, the preceding regular expression must match exactly @var{n} times |
is, the preceding regular expression must match exactly @var{n} times |
479 |
in a row. For example, @samp{x\@{4\@}} matches the string @samp{xxxx} |
in a row. For example, @samp{x\@{4\@}} matches the string @samp{xxxx} |
480 |
and nothing else. |
and nothing else. |
481 |
|
|
482 |
@item \@{@var{n},@var{m}\@} |
@item @kbd{\@{@var{n},@var{m}\@}} |
483 |
is a postfix operator that specifies repetition between @var{n} and |
is a postfix operator that specifies repetition between @var{n} and |
484 |
@var{m} times---that is, the preceding regular expression must match |
@var{m} times---that is, the preceding regular expression must match |
485 |
at least @var{n} times, but no more than @var{m} times. If @var{m} is |
at least @var{n} times, but no more than @var{m} times. If @var{m} is |
488 |
equivalent to @samp{?}. @* @samp{\@{0,\@}} is equivalent to |
equivalent to @samp{?}. @* @samp{\@{0,\@}} is equivalent to |
489 |
@samp{*}. @* @samp{\@{1,\@}} is equivalent to @samp{+}. |
@samp{*}. @* @samp{\@{1,\@}} is equivalent to @samp{+}. |
490 |
|
|
491 |
@item [ @dots{} ] |
@item @kbd{[ @dots{} ]} |
492 |
is a @dfn{character set}, which begins with @samp{[} and is terminated |
is a @dfn{character set}, which begins with @samp{[} and is terminated |
493 |
by @samp{]}. In the simplest case, the characters between the two |
by @samp{]}. In the simplest case, the characters between the two |
494 |
brackets are what this set can match. |
brackets are what this set can match. |
523 |
be non-letters. The behavior of a mixed-case range such as @samp{A-z} |
be non-letters. The behavior of a mixed-case range such as @samp{A-z} |
524 |
is somewhat ill-defined, and it may change in future Emacs versions. |
is somewhat ill-defined, and it may change in future Emacs versions. |
525 |
|
|
526 |
@item [^ @dots{} ] |
@item @kbd{[^ @dots{} ]} |
527 |
@samp{[^} begins a @dfn{complemented character set}, which matches any |
@samp{[^} begins a @dfn{complemented character set}, which matches any |
528 |
character except the ones specified. Thus, @samp{[^a-z0-9A-Z]} matches |
character except the ones specified. Thus, @samp{[^a-z0-9A-Z]} matches |
529 |
all characters @emph{except} ASCII letters and digits. |
all characters @emph{except} ASCII letters and digits. |
536 |
mentioned as one of the characters not to match. This is in contrast to |
mentioned as one of the characters not to match. This is in contrast to |
537 |
the handling of regexps in programs such as @code{grep}. |
the handling of regexps in programs such as @code{grep}. |
538 |
|
|
539 |
@item ^ |
@item @kbd{^} |
540 |
is a special character that matches the empty string, but only at the |
is a special character that matches the empty string, but only at the |
541 |
beginning of a line in the text being matched. Otherwise it fails to |
beginning of a line in the text being matched. Otherwise it fails to |
542 |
match anything. Thus, @samp{^foo} matches a @samp{foo} that occurs at |
match anything. Thus, @samp{^foo} matches a @samp{foo} that occurs at |
543 |
the beginning of a line. |
the beginning of a line. |
544 |
|
|
545 |
@item $ |
@item @kbd{$} |
546 |
is similar to @samp{^} but matches only at the end of a line. Thus, |
is similar to @samp{^} but matches only at the end of a line. Thus, |
547 |
@samp{x+$} matches a string of one @samp{x} or more at the end of a line. |
@samp{x+$} matches a string of one @samp{x} or more at the end of a line. |
548 |
|
|
549 |
@item \ |
@item @kbd{\} |
550 |
has two functions: it quotes the special characters (including |
has two functions: it quotes the special characters (including |
551 |
@samp{\}), and it introduces additional special constructs. |
@samp{\}), and it introduces additional special constructs. |
552 |
|
|