home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / unix / regexp.sit / manpage next >
Text File  |  1991-04-10  |  8KB  |  265 lines

  1.  
  2.  
  3.  
  4.      REGEXP(3)             UNIX System V (local)             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
  44.           following (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
  53.           substring and the endp pointing to the first character
  54.           following the substring.  The 0th substring is the substring
  55.           of string that matched the whole regular expression.  The
  56.           others are those substrings that matched parenthesized
  57.           expressions within the regular expression, with
  58.           parenthesized expressions numbered in left-to-right order of
  59.           their opening parentheses.
  60.  
  61.  
  62.  
  63.      Page 1                                          (printed 4/10/91)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      REGEXP(3)             UNIX System V (local)             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
  77.           indicated 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 `\'
  108.           followed by a single character (matching that character), or
  109.           a 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
  115.           character not from the rest of the sequence.  If two
  116.           characters in the sequence are separated by `-', this is
  117.           shorthand for the full list of ASCII characters between them
  118.           (e.g. `[0-9]' matches any decimal digit).  To include a
  119.           literal `]' in the sequence, make it the first character
  120.           (following a possible `^').  To include a literal `-', make
  121.           it the first or last 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.      Page 2                                          (printed 4/10/91)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      REGEXP(3)             UNIX System V (local)             REGEXP(3)
  137.  
  138.  
  139.  
  140.           earliest.  If both begin in the same place    but match
  141.           different lengths, or match the same length in different
  142.           ways, life gets messier, as follows.
  143.  
  144.           In general, the possibilities in a list of branches are
  145.           considered in left-to-right order, the possibilities for
  146.           `*', `+', and `?' are considered longest-first, nested
  147.           constructs are considered from the outermost in, and
  148.           concatenated constructs are considered leftmost-first.  The
  149.           match that will be chosen is the one that uses the earliest
  150.           possibility in the first choice that has to be made.  If
  151.           there is more than one choice, the next will be made in the
  152.           same manner (earliest possibility) subject to the decision
  153.           on the first 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
  168.           decision on where to start the match is the first choice to
  169.           be made, hence subsequent choices must respect it even if
  170.           this 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
  187.           portable to V8.
  188.  
  189.           The restriction against applying `*' or `+' to a possibly-
  190.           null operand is an artifact of the simplistic
  191.           implementation.
  192.  
  193.  
  194.  
  195.      Page 3                                          (printed 4/10/91)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      REGEXP(3)             UNIX System V (local)             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.      Page 4                                          (printed 4/10/91)
  262.  
  263.  
  264.  
  265.