home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / 09_02.iso / software / mp3ext / MP3ext33b19.exe / {app} / rx.info < prev   
Encoding:
GNU Info File  |  1998-05-20  |  13.4 KB  |  502 lines

  1. This is Info file rx.info, produced by Makeinfo-1.63 from the input
  2. file rx.texi.
  3.  
  4. 
  5. File: rx.info,  Node: Top,  Next: An Introduction to Regexps,  Prev: (dir),  Up: (dir)
  6.  
  7. Regexps
  8. *******
  9.  
  10.    This document describes the Posix "Basic Regular Expression" ("BRE")
  11. language.
  12.  
  13.    The Posix Basic Regular Expression language is a notation for
  14. describing text patterns.  Regexps are typically used by comparing them
  15. to a string to see if that string matches the pattern, or by searching
  16. within a string for a substring that matches.
  17.  
  18.    This is not a formal definition of Posix regexps - it is an intuitive
  19. and hopefully expository description of them.
  20.  
  21. * Menu:
  22.  
  23. * An Introduction to Regexps::
  24. * Literal Regexps::
  25. * Character Sets::
  26. * Subexpressions::
  27. * Repeated Subexpressions::
  28. * Optional Subexpressions::
  29. * Counted Subexpressions::
  30. * Alternative Subexpressions::
  31. * Backreferences::
  32. * A Summary of Regexp Syntax::
  33. * Ambiguous Patterns::
  34. * Acknowledgements::
  35.  
  36. 
  37. File: rx.info,  Node: An Introduction to Regexps,  Next: Literal Regexps,  Prev: Top,  Up: Top
  38.  
  39. An Introduction to Regexps
  40. ==========================
  41.  
  42.    In the simplest cases, a regexp is just a literal string that must
  43. match exactly.   For example, the pattern:
  44.  
  45.      regexp
  46.  
  47. matches the string "regexp" and no others.
  48.  
  49.    Some characters have a special meaning when they occur in a regexp.
  50. They aren't matched literally as in the previous example, but instead
  51. denote a more general pattern.   For example, the character `*' is used
  52. to indicate that the preceeding element of a regexp may be repeated 0,
  53. 1, or more times.  In the pattern:
  54.  
  55.      smooo*th
  56.  
  57. the `*' indicates that the preceeding `o' can be repeated 0 or more
  58. times.   So the pattern matches:
  59.  
  60.      smooth
  61.      smoooth
  62.      smooooth
  63.      smoooooth
  64.      ...
  65.  
  66. Suppose you want to write a pattern that literally matches a special
  67. character like `*' - in other words, you don't want to `*' to indicate
  68. a permissible repetition, but to match `*' literally.  This is
  69. accomplished by quoting the special character with a backslash.  The
  70. pattern:
  71.  
  72.      smoo\*th
  73.  
  74. matches the string:
  75.  
  76.      smoo*th
  77.  
  78. and no other strings.
  79.  
  80.    In seven cases, the pattern is reversed - a backslash makes the
  81. character special instead of making a special character normal.  The
  82. characters `+', `?', `|', `(', and `)' are normal but the sequences
  83. `\+', `\?', `\|', `\(', `\)', `\{', and `\}' are special (their meaning
  84. is described later).
  85.  
  86.    The remaining sections of this section introduce and explain the
  87. various special characters that can occur in regexps.
  88.  
  89. 
  90. File: rx.info,  Node: Literal Regexps,  Next: Character Sets,  Prev: An Introduction to Regexps,  Up: Top
  91.  
  92. Literal Regexps
  93. ===============
  94.  
  95.    A literal regexp is a string which contains no special characters.
  96. A literal regexp matches an identical string, but no other characters.
  97. For example:
  98.  
  99.      literally
  100.  
  101. matches
  102.  
  103.      literally
  104.  
  105. and nothing else.
  106.  
  107.    Generally, whitespace characters, numbers, and letters are not
  108. special.  Some punctuation characters are special and some are not (the
  109. syntax summary at the end of this section makes a convenient reference
  110. for which characters are special and which aren't).
  111.  
  112. 
  113. File: rx.info,  Node: Character Sets,  Next: Subexpressions,  Prev: Literal Regexps,  Up: Top
  114.  
  115. Character Sets
  116. ==============
  117.  
  118.    This section introduces the special characters `.' and `['.
  119.  
  120.    `.' matches any character except the NULL character.  For example:
  121.  
  122.      p.ck
  123.  
  124. matches
  125.  
  126.      pick
  127.      pack
  128.      puck
  129.      pbck
  130.      pcck
  131.      p.ck
  132.      
  133.      ...
  134.  
  135. `[' begins a "character set".  A character set is similar to `.' in
  136. that it matches not a single, literal character, but any of a set of
  137. characters.   `[' is different from `.' in that with `[', you define
  138. the set of characters explicitly.
  139.  
  140.    There are three basic forms a character set can take.
  141.  
  142.    In the first form, the character set is spelled out:
  143.  
  144.      [<cset-spec>]    -- every character in <cset-spec> is in the set.
  145.  
  146. In the second form, the character set indicated is the negation of a
  147. character set is explicitly spelled out:
  148.  
  149.      [^<cset-spec>]    -- every character *not* in <cset-spec> is in the set.
  150.  
  151. A `<cset-spec>' is more or less an explicit enumeration of a set of
  152. characters.  It can be written as a string of individual characters:
  153.  
  154.      [aeiou]
  155.  
  156. or as a range of characters:
  157.  
  158.      [0-9]
  159.  
  160. These two forms can be mixed:
  161.  
  162.      [A-za-z0-9_$]
  163.  
  164.    Note that special regexp characters (such as `*') are *not* special
  165. within a character set.  `-', as illustrated above, *is* special,
  166. except, as illustrated below, when it is the first character mentioned.
  167.  
  168.    This is a four-character set:
  169.  
  170.      [-+*/]
  171.  
  172.    The third form of a character set makes use of a pre-defined
  173. "character class":
  174.  
  175.      [[:class-name:]] -- every character described by class-name is in the set.
  176.  
  177.    The supported character classes are:
  178.  
  179.      alnum    - the set of alpha-numeric characters
  180.      alpha    - the set of alphabetic characters
  181.      blank    - tab and space
  182.      cntrl    - the control characters
  183.      digit    - decimal digits
  184.      graph    - all printable characters except space
  185.      lower    - lower case letters
  186.      print    - the "printable" characters
  187.      punct    - punctuation
  188.      space    - whitespace characters
  189.      upper    - upper case letters
  190.      xdigit    - hexidecimal digits
  191.  
  192.    Finally, character class sets can also be inverted:
  193.  
  194.      [^[:space:]] - all non-whitespace characters
  195.  
  196.    Character sets can be used in a regular expression anywhere a literal
  197. character can.
  198.  
  199. 
  200. File: rx.info,  Node: Subexpressions,  Next: Repeated Subexpressions,  Prev: Character Sets,  Up: Top
  201.  
  202. Subexpressions
  203. ==============
  204.  
  205.    A subexpression is a regular expression enclosed in `\(' and `\)'.
  206. A subexpression can be used anywhere a single character or character
  207. set can be used.
  208.  
  209.    Subexpressions are useful for grouping regexp constructs.  For
  210. example, the repeat operator, `*', usually applies to just the
  211. preceeding character.   Recall that:
  212.  
  213.      smooo*th
  214.  
  215. matches
  216.  
  217.      smooth
  218.      smoooth
  219.      ...
  220.  
  221.    Using a subexpression, we can apply `*' to a longer string:
  222.  
  223.      banan\(an\)*a
  224.  
  225. matches
  226.  
  227.      banana
  228.      bananana
  229.      banananana
  230.      ...
  231.  
  232.    Subexpressions also have a special meaning with regard to
  233. backreferences and substitutions (see *Note Backreferences::).
  234.  
  235. 
  236. File: rx.info,  Node: Repeated Subexpressions,  Next: Optional Subexpressions,  Prev: Subexpressions,  Up: Top
  237.  
  238. Repeated Subexpressions
  239. =======================
  240.  
  241.    `*' is the repeat operator.  It applies to the preceeding character,
  242. character set, subexpression or backreference.  It indicates that the
  243. preceeding element can be matched 0 or more times:
  244.  
  245.      bana\(na\)*
  246.  
  247. matches
  248.  
  249.      bana
  250.      banana
  251.      bananana
  252.      banananana
  253.      ...
  254.  
  255. `\+' is similar to `*' except that `\+' requires the preceeding element
  256. to be matched at least once.  So while:
  257.  
  258.      bana\(na\)*
  259.  
  260. matches
  261.  
  262.      bana
  263.  
  264.      bana(na\)\+
  265.  
  266. does not.   Both match
  267.  
  268.      banana
  269.      bananana
  270.      banananana
  271.      ...
  272.  
  273. Thus, `bana\(na\)+' is short-hand for `banana\(na\)*'.
  274.  
  275. 
  276. File: rx.info,  Node: Optional Subexpressions,  Next: Counted Subexpressions,  Prev: Repeated Subexpressions,  Up: Top
  277.  
  278. Optional Subexpressions
  279. =======================
  280.  
  281.    `\?' indicates that the preceeding character, character set, or
  282. subexpression is optional.  It is permitted to match, or to be skipped:
  283.  
  284.      CSNY\?
  285.  
  286. matches both
  287.  
  288.      CSN
  289.  
  290. and
  291.  
  292.      CSNY
  293.  
  294. 
  295. File: rx.info,  Node: Counted Subexpressions,  Next: Alternative Subexpressions,  Prev: Optional Subexpressions,  Up: Top
  296.  
  297. Counted Subexpressions
  298. ======================
  299.  
  300.    An interval expression, `\{m,n\}' where `m' and `n' are non-negative
  301. integers with `n >= m', applies to the preceeding character, character
  302. set, subexpression or backreference.  It indicates that the preceeding
  303. element must match at least `m' times and may match as many as `n'
  304. times.
  305.  
  306.    For example:
  307.  
  308.      c\([ad]\)\{1,4\}
  309.  
  310. matches
  311.  
  312.      car
  313.      cdr
  314.      caar
  315.      cdar
  316.      ...
  317.      caaar
  318.      cdaar
  319.      ...
  320.      cadddr
  321.      cddddr
  322.  
  323. 
  324. File: rx.info,  Node: Alternative Subexpressions,  Next: Backreferences,  Prev: Counted Subexpressions,  Up: Top
  325.  
  326. Alternative Subexpressions
  327. ==========================
  328.  
  329.    An alternative is written:
  330.  
  331.      regexp-1\|regexp-2\|regexp-3\|...
  332.  
  333. It matches anything matched by some `regexp-n'.  For example:
  334.  
  335.      Crosby, Stills, \(and Nash\|Nash, and Young\)
  336.  
  337. matches
  338.  
  339.      Crosby, Stills, and Nash
  340.  
  341. and
  342.  
  343.      Crosby, Stills, Nash, and Young
  344.  
  345. 
  346. File: rx.info,  Node: Backreferences,  Next: A Summary of Regexp Syntax,  Prev: Alternative Subexpressions,  Up: Top
  347.  
  348. Backreferences, Extractions and Substitutions
  349. =============================================
  350.  
  351.    A backreference is written `\n' where `n' is some single digit other
  352. than 0.  To be a valid backreference, there must be at least `n'
  353. parenthesized subexpressions in the pattern prior to the backreference.
  354.  
  355.    A backreference matches a literal copy of whatever was matched by the
  356. corresponding subexpression.  For example,
  357.  
  358.      \(.*\)-\1
  359.  
  360. matches:
  361.  
  362.      go-go
  363.      ha-ha
  364.      wakka-wakka
  365.      ...
  366.  
  367.    In some applications, subexpressions are used to extract substrings.
  368. For example, Emacs has the functions `match-beginnning' and `match-end'
  369. which report the positions of strings matched by subexpressions.  These
  370. functions use the same numbering scheme for subexpressions as
  371. backreferences, with the additional rule that subexpression 0 is
  372. defined to be the whole regexp.
  373.  
  374.    In some applications, subexpressions are used in string substitution.
  375. This again uses the backreference numbering scheme. For example, this
  376. sed command:
  377.  
  378.      s/From:.*<\(.*\)>/To: \1/
  379.  
  380. first matches the line:
  381.  
  382.      From: Joe Schmoe <schmoe@uspringfield.edu>
  383.  
  384. when it does, subexpression 1 matches "schmoe@uspringfield.edu".  The
  385. command replaces the matched line with "To: \1" after doing
  386. subexpression substitution on it to get:
  387.  
  388.      To: schmoe@uspringfield.edu
  389.  
  390. 
  391. File: rx.info,  Node: A Summary of Regexp Syntax,  Next: Ambiguous Patterns,  Prev: Backreferences,  Up: Top
  392.  
  393. A Summary of Regexp Syntax
  394. ==========================
  395.  
  396.    In summary, regexps can be:
  397.  
  398.    `abcd' - matching a string literally
  399.  
  400.    `.' - matching everything except NULL
  401.  
  402.    `[a-z_?]', `^[a-z_?]', `[[:alpha:]]' and `[^[:alpha:]]' - matching
  403. character sets
  404.  
  405.    `\(subexp\)' - grouping an expression into a subexpression.
  406.  
  407.    `\n' - match a copy of whatever was matched by the nth subexpression.
  408.  
  409.    The following special characters and sequences can be applied to a
  410. character, character set, subexpression, or backreference:
  411.  
  412.    `*' - repeat the preceeding element 0 or more times.
  413.  
  414.    `\+' - repeat the preceeding element 1 or more times.
  415.  
  416.    `\?' - match the preceeding element 0 or 1 time.
  417.  
  418.    `{m,n}' - match the preceeding element at least `m', and as many as
  419. `n' times.
  420.  
  421.    `regexp-1\|regexp-2\|..' - match any regexp-n.
  422.  
  423.    A special character, like `.' or `*' can be made into a literal
  424. character by prefixing it with `\'.
  425.  
  426.    A special sequence, like `\+' or `\?' can be made into a literal
  427. character by dropping the `\'.
  428.  
  429. 
  430. File: rx.info,  Node: Ambiguous Patterns,  Next: Acknowledgements,  Prev: A Summary of Regexp Syntax,  Up: Top
  431.  
  432. Ambiguous Patterns
  433. ==================
  434.  
  435.    Sometimes a regular expression appears to be ambiguous.  For
  436. example, suppose we compare the pattern:
  437.  
  438.      begin\|beginning
  439.  
  440. to the string
  441.  
  442.      beginning
  443.  
  444. either just the first 5 characters will match, or the whole string will
  445. match.
  446.  
  447.    In every case like this, the longer match is preferred.  The whole
  448. string will match.
  449.  
  450.    Sometimes there is ambiguity not about how many characters to match,
  451. but where the subexpressions occur within the match.  This can effect
  452. extraction functions like Emacs' `match-beginning' or rewrite functions
  453. like sed's `s' command.  For example, consider matching the pattern:
  454.  
  455.      b\(\[^q]*\)\(ing\)?
  456.  
  457. against the string
  458.  
  459.      beginning
  460.  
  461.    One possibility is that the first subexpression matches "eginning"
  462. and the second is skipped.  Another possibility is that the first
  463. subexpression matches "eginn" and the second matches "ing".
  464.  
  465.    The rule is that consistant with matching as many characters as
  466. possible, the length of lower numbered subexpressions is maximized in
  467. preference to maximizing the length of later subexpressions.
  468.  
  469.    In the case of the above example, the two possible matches are equal
  470. in overall length.  Therefore, it comes down to maximizing the
  471. lower-numbered subexpression, \1.  The correct answer is that \1 matches
  472. "eginning" and \2 is skipped.
  473.  
  474. 
  475. File: rx.info,  Node: Acknowledgements,  Prev: Ambiguous Patterns,  Up: Top
  476.  
  477. Acknowledgements
  478. ================
  479.  
  480.    This work was created with support from "Cygnus Solutions Inc." and
  481. "The Free Software Foundation, Inc.".  Support the GNU project.  Support
  482. free software!
  483.  
  484.  
  485. 
  486. Tag Table:
  487. Node: Top83
  488. Node: An Introduction to Regexps955
  489. Node: Literal Regexps2556
  490. Node: Character Sets3175
  491. Node: Subexpressions5485
  492. Node: Repeated Subexpressions6274
  493. Node: Optional Subexpressions7021
  494. Node: Counted Subexpressions7385
  495. Node: Alternative Subexpressions7998
  496. Node: Backreferences8438
  497. Node: A Summary of Regexp Syntax9898
  498. Node: Ambiguous Patterns11040
  499. Node: Acknowledgements12504
  500. 
  501. End Tag Table
  502.