home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / ed.zip / regexp.man < prev    next >
Text File  |  1989-12-15  |  8KB  |  265 lines

  1.  
  2.  
  3.  
  4.                                                         REGEXP(3)
  5.  
  6.  
  7.  
  8. NAME
  9.      regcomp, regexec, regsub, regerror - regular expression
  10.      handler
  11.  
  12. SYNOPSIS
  13.      #include <regexp.h>
  14.  
  15.      regexp *regcomp(exp)
  16.      char *exp;
  17.  
  18.      int regexec(prog, string)
  19.      regexp *prog;
  20.      char *string;
  21.  
  22.      regsub(prog, source, dest)
  23.      regexp *prog;
  24.      char *source;
  25.      char *dest;
  26.  
  27.      regerror(msg)
  28.      char *msg;
  29.  
  30. DESCRIPTION
  31.      These functions implement egrep(1)-style regular expressions
  32.      and supporting facilities.
  33.  
  34.      Regcomp compiles a regular expression into a structure of
  35.      type regexp, and returns a pointer to it.  The space has
  36.      been allocated using malloc(3) and may be released by free.
  37.  
  38.      Regexec matches a NUL-terminated string against the compiled
  39.      regular expression in prog.  It returns 1 for success and 0
  40.      for failure, and adjusts the contents of prog's startp and
  41.      endp (see below) accordingly.
  42.  
  43.      The members of a regexp structure include at least the fol-
  44.      lowing (not necessarily in order):
  45.  
  46.           char *startp[NSUBEXP];
  47.           char *endp[NSUBEXP];
  48.  
  49.      where NSUBEXP is defined (as 10) in the header file.  Once a
  50.      successful regexec has been done using the regexp, each
  51.      startp-endp pair describes one substring within the string,
  52.      with the startp pointing to the first character of the sub-
  53.      string and the endp pointing to the first character follow-
  54.      ing the substring.  The 0th substring is the substring of
  55.      string that matched the whole regular expression.  The oth-
  56.      ers are those substrings that matched parenthesized expres-
  57.      sions within the regular expression, with parenthesized
  58.      expressions numbered in left-to-right order of their opening
  59.      parentheses.
  60.  
  61.  
  62.  
  63.                                                                 1
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. REGEXP(3)
  71.  
  72.  
  73.  
  74.      Regsub copies source to dest, making substitutions according
  75.      to the most recent regexec performed using prog.  Each
  76.      instance of `&' in source is replaced by the substring indi-
  77.      cated by startp[0] and endp[0].  Each instance of `\n',
  78.      where n is a digit, is replaced by the substring indicated
  79.      by startp[n] and endp[n].  To get a literal `&' or `\n' into
  80.      dest, prefix it with `\'; to get a literal `\' preceding `&'
  81.      or `\n', prefix it with another `\'.
  82.  
  83.      Regerror is called whenever an error is detected in regcomp,
  84.      regexec, or regsub.  The default regerror writes the string
  85.      msg, with a suitable indicator of origin, on the standard
  86.      error output and invokes exit(2).  Regerror can be replaced
  87.      by the user if other actions are desirable.
  88.  
  89. REGULAR EXPRESSION SYNTAX
  90.      A regular expression is zero or more branches, separated by
  91.      `|'.  It matches anything that matches one of the branches.
  92.  
  93.      A branch is zero or more pieces, concatenated.  It matches a
  94.      match for the first, followed by a match for the second,
  95.      etc.
  96.  
  97.      A piece is an atom possibly followed by `*', `+', or `?'.
  98.      An atom followed by `*' matches a sequence of 0 or more
  99.      matches of the atom.  An atom followed by `+' matches a
  100.      sequence of 1 or more matches of the atom.  An atom followed
  101.      by `?' matches a match of the atom, or the null string.
  102.  
  103.      An atom is a regular expression in parentheses (matching a
  104.      match for the regular expression), a range (see below), `.'
  105.      (matching any single character), `^' (matching the null
  106.      string at the beginning of the input string), `$' (matching
  107.      the null string at the end of the input string), a `\' fol-
  108.      lowed by a single character (matching that character), or a
  109.      single character with no other significance (matching that
  110.      character).
  111.  
  112.      A range is a sequence of characters enclosed in `[]'.  It
  113.      normally matches any single character from the sequence.  If
  114.      the sequence begins with `^', it matches any single charac-
  115.      ter not from the rest of the sequence.  If two characters in
  116.      the sequence are separated by `-', this is shorthand for the
  117.      full list of ASCII characters between them (e.g. `[0-9]'
  118.      matches any decimal digit).  To include a literal `]' in the
  119.      sequence, make it the first character (following a possible
  120.      `^').  To include a literal `-', make it the first or last
  121.      character.
  122.  
  123. AMBIGUITY
  124.      If a regular expression could match two different parts of
  125.      the input string, it will match the one which begins
  126.  
  127.  
  128.  
  129. 2
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.                                                         REGEXP(3)
  137.  
  138.  
  139.  
  140.      earliest.  If both begin in the same place    but match dif-
  141.      ferent lengths, or match the same length in different ways,
  142.      life gets messier, as follows.
  143.  
  144.      In general, the possibilities in a list of branches are con-
  145.      sidered in left-to-right order, the possibilities for `*',
  146.      `+', and `?' are considered longest-first, nested constructs
  147.      are considered from the outermost in, and concatenated con-
  148.      structs are considered leftmost-first.  The match that will
  149.      be chosen is the one that uses the earliest possibility in
  150.      the first choice that has to be made.  If there is more than
  151.      one choice, the next will be made in the same manner (earli-
  152.      est possibility) subject to the decision on the first
  153.      choice.  And so forth.
  154.  
  155.      For example, `(ab|a)b*c' could match `abc' in one of two
  156.      ways.  The first choice is between `ab' and `a'; since `ab'
  157.      is earlier, and does lead to a successful overall match, it
  158.      is chosen.  Since the `b' is already spoken for, the `b*'
  159.      must match its last possibility-the empty string-since it
  160.      must respect the earlier choice.
  161.  
  162.      In the particular case where no `|'s are present and there
  163.      is only one `*', `+', or `?', the net effect is that the
  164.      longest possible match will be chosen.  So `ab*', presented
  165.      with `xabbbby', will match `abbbb'.  Note that if `ab*' is
  166.      tried against `xabyabbbz', it will match `ab' just after
  167.      `x', due to the begins-earliest rule.  (In effect, the deci-
  168.      sion on where to start the match is the first choice to be
  169.      made, hence subsequent choices must respect it even if this
  170.      leads them to less-preferred alternatives.)
  171.  
  172. SEE ALSO
  173.      egrep(1), expr(1)
  174.  
  175. DIAGNOSTICS
  176.      Regcomp returns NULL for a failure (regerror permitting),
  177.      where failures are syntax errors, exceeding implementation
  178.      limits, or applying `+' or `*' to a possibly-null operand.
  179.  
  180. HISTORY
  181.      Both code and manual page were written at U of T.  They are
  182.      intended to be compatible with the Bell V8 regexp(3), but
  183.      are not derived from Bell code.
  184.  
  185. BUGS
  186.      Empty branches and empty regular expressions are not port-
  187.      able to V8.
  188.  
  189.      The restriction against applying `*' or `+' to a possibly-
  190.      null operand is an artifact of the simplistic implementa-
  191.      tion.
  192.  
  193.  
  194.  
  195.                                                                 3
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. REGEXP(3)
  203.  
  204.  
  205.  
  206.      Does not support egrep's newline-separated branches; neither
  207.      does the V8 regexp(3), though.
  208.  
  209.      Due to emphasis on compactness and simplicity, it's not
  210.      strikingly fast.  It does give special attention to handling
  211.      simple cases quickly.
  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. 4
  262.  
  263.  
  264.  
  265.