home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 17 / CD_ASCQ_17_101194.iso / vrac / regexpr.zip / REGEXPR.TXT < prev   
Text File  |  1994-09-19  |  22KB  |  535 lines

  1. Regular Expressions
  2.  
  3.             What, you  ask, are  regular expressions?  Considered in one
  4.             sense, they  are no  more than  the wildcard  characters '*'
  5.             (match  zero   or  more   characters)  and  '?'  (match  one
  6.             character) that  are used  by many operating systems for the
  7.             specification of  filenames.   However, they are vastly more
  8.             powerful and flexible than these two simple examples.
  9.  
  10.             Regular expressions consist of a combination of literal text
  11.             characters and regular expression "metacharacters".  Here is
  12.             the full  complement of  regular  expression  metacharacters
  13.             recognized by EGREP:
  14.  
  15.                 Symbol          Name
  16.                 --------------------
  17.  
  18.                 \               Escape
  19.                 "               Quotation
  20.                 .               Any
  21.                 ^               Line begin (or character class negation)
  22.                 $               Line end
  23.                 [               Character class begin
  24.                 -               Character class range separator
  25.                 ]               Character class end
  26.                 (               Group begin
  27.                 )               Group end
  28.                 ?               Option
  29.                 *               Closure
  30.                 +               Positive closure
  31.                 {               Iteration begin
  32.                 ,               Iteration parameter separator
  33.                 }               Iteration end
  34.                 |               Alternation
  35.  
  36.  
  37.             and the following are examples of their usage:
  38.  
  39.                 Expression      Matches                         Example
  40.                 -------------------------------------------------------
  41.  
  42.                 c               Any literal character           a
  43.                 \c              Character c literally           \*
  44.                 "s"             String s literally              "**"
  45.                 .               Any character but newline       a.b
  46.                 ^               Beginning of line               ^abc
  47.                 $               End of line                     abc$
  48.                 [s]             Any character in s              [abc]
  49.                 [^s]            Any character not in s          [^abc]
  50.  
  51.  
  52.  
  53.                 r1r2            r1 followed by r2               ab
  54.                 r?              Zero or one r's                 a?
  55.                 r*              Zero or more r's                a*
  56.                 r+              One or more r's                 a+
  57.                 r{m,n}          m to n occurrences of r         a{2,5}
  58.                 r1|r2           r1 or r2                        a|b
  59.                 (r)             r                               (a|b)
  60.  
  61.  
  62.             Still  confused?     Let's   discuss  each   of  the   above
  63.             metacharacters in detail.
  64.  
  65.  
  66.         1.1.  Literal Characters
  67.  
  68.             At its  simplest level,  a regular  expression  consists  of
  69.             nothing but  literal characters.   Unless a character is one
  70.             of the  regular expression  metacharacters shown  above,  it
  71.             represents itself  as a  member of  the ASCII  character set
  72.             when used in a regular expression.
  73.  
  74.  
  75.         1.2.  Escape Characters
  76.  
  77.             There will  be times  when you  want to  include  a  regular
  78.             expression  metacharacter  in  a  regular  expression  as  a
  79.             literal character.   Also,  you may  want  to  include  non-
  80.             printable characters.   This  is where escape characters are
  81.             essential.
  82.  
  83.             EGREP recognizes the following escape characters:
  84.  
  85.                 Expression      Matches         Hexadecimal Equivalent
  86.                 ------------------------------------------------------
  87.  
  88.                 \n              newline                 0x0A
  89.                 \t              horizontal tab          0x09
  90.                 \b              backspace               0x08
  91.                 \r              carriage return         0x0D
  92.                 \f              formfeed                0x0C
  93.                 \ddd            octal digit             0x00 to 0x7F
  94.                 \xhhh           hexadecimal digit       0x00 to 0x7F
  95.                 \c              anything else           0x20 to 0x7E
  96.  
  97.                 where:
  98.  
  99.                 1. Each 'd' of an octal digit is a printable digit
  100.                    from the character set "01234567".  There can be
  101.                    between 1 and 3 digits.
  102.  
  103.  
  104.  
  105.                 2. Each 'h' of a hexadecimal digit is a printable digit
  106.                    from the character set "0123456789ABCDEFabcdef".  The
  107.                    leading 'x' must be in lower case.  There can be
  108.                    between 1 and 3 digits.
  109.  
  110.                 1. 'c' is any printable value not included in the set of
  111.                    escape characters shown above, including '\' itself
  112.                    (i.e. - "\\").
  113.  
  114.                 NOTE: Octal and hexadecimal escape characters are not
  115.                       supported by UNIX EGREP.
  116.  
  117.  
  118.             In general,  specifying characters in regular expressions as
  119.             octal or  hexadecimal digits  is  implementation  dependent.
  120.             Using EGREP  would not  produce the same results on a system
  121.             using the  ASCII character  set and another using the EBCDIC
  122.             character set, for example.
  123.  
  124.             byHeart Software's  implementation of  EGREP uses  the ASCII
  125.             character  set.    This  means  that  you  can  specify  any
  126.             character between 0x00 and 0x7F in a regular expression.
  127.  
  128.             There are  however two  special cases  to be considered: the
  129.             "newline" character 0x0A and the NULL character 0x00.  EGREP
  130.             will not  match any  pattern containing  a newline character
  131.             unless that  character  is  the  last  one  in  the  regular
  132.             expression. The  "line end" metacharacter ('$') (see Section
  133.             1.6,  "Line   End")  is   more  commonly  used  in  such  an
  134.             application.
  135.  
  136.             EGREP will accept the NULL character when it is specified in
  137.             a  regular  expression.  However,  it  uses  this  character
  138.             internally to  represent the  end of each text line it reads
  139.             in for  processing. If  the line  contains a NULL character,
  140.             EGREP will  consider it to be the end of the line and ignore
  141.             the remaining characters when searching for pattern matches.
  142.  
  143.  
  144.         1.1.  Literal Strings
  145.  
  146.             Occasionally,  you   may  want   to  specify   a  string  of
  147.             metacharacters in a regular expression.  One example of this
  148.             would be to search for "*****".  Since it is inconvenient to
  149.             express this  as "\*\*\*\*\*",  EGREP allows  you to specify
  150.             literal   strings   of   characters   (either   literal   or
  151.             metacharacters) by  enclosing them  in double  quote  marks.
  152.             For example:
  153.  
  154.  
  155.  
  156.                 "*** This is a literal string ***"
  157.  
  158.             WARNING: The  above example  will work  only if  the regular
  159.             expression is  taken from a file using the '-f' command-line
  160.             switch.  The double quote character ('"') is used by the MS-
  161.             DOS command-line  processor to delimit quoted arguments (see
  162.             Section 2.5,  "Entering Regular  Expressions").  If you want
  163.             to use  it  as  a  regular  expression  metacharacter  in  a
  164.             command-line argument,  you must  use its  escape  character
  165.             equivalent, as in:
  166.  
  167.                 "The use of \"*+?$\" as literals is possible"
  168.  
  169.             Escape  characters   are  also   recognized  inside  literal
  170.             strings, so  that the  double  quote  metacharacter  can  be
  171.             expressed as a literal character.  For example:
  172.  
  173.                 "The '\"' symbol is a metacharacter."
  174.  
  175.             NOTE: Literal strings are not supported by UNIX EGREP.
  176.  
  177.  
  178.         1.4.  Any Character
  179.  
  180.             The "any"  metacharacter (a period) can be used to match any
  181.             character except "newline".  For example:
  182.  
  183.                 ab.cd
  184.  
  185.             will match "abccd", "ab cd", "ab.cd", and so on.
  186.  
  187.  
  188.         1.5.  Line Begin
  189.  
  190.             If  the  "line  begin"  metacharacter  ('^')  is  the  FIRST
  191.             character in  a regular  expression, a string in a line will
  192.             be recognized as matching the rest of the regular expression
  193.             only if that string occurs at the beginning of the line.
  194.  
  195.             If the  metacharacter occurs  anywhere else  in the  regular
  196.             expression outside  of a  character class  (see Section 1.7,
  197.             "Character Classes"),  it is treated as a literal character.
  198.             Thus:
  199.  
  200.                 ^abc
  201.  
  202.             will match "abcdef" but not "aabcdef", and
  203.  
  204.                 ab^c
  205.  
  206.             will match "ab^c", "xyzab^c" and so on.
  207.  
  208.  
  209.         1.6.  Line End
  210.  
  211.             Analogous to  the "line begin" metacharacter, the "line end"
  212.             metacharacter ('$')  is only  recognized as  such when it is
  213.             the LAST character in a regular expression.  Thus:
  214.  
  215.                 abc$
  216.  
  217.             will match "xyzabc" but not "abcdef", and
  218.  
  219.                 a$bc
  220.  
  221.             will match "a$bc", "xyza$bc" and so on.
  222.  
  223.  
  224.         1.7.  Character Classes
  225.  
  226.             This is  where the  expressive power  of regular expressions
  227.             becomes apparent.   Whereas  the  "any"  metacharacter  will
  228.             match any  literal character  except "newline",  a character
  229.             class can  be used to specify any character in (or not in) a
  230.             class of characters.  Here are some examples:
  231.  
  232.                 [abc]       matches either 'a', 'b' or 'c'
  233.                 [^abc]      matches any character but 'a', 'b' or 'c'
  234.                 [a^bc]      matches 'a', 'b', 'c' or '^'
  235.                 [a-yD-M]    matches any character in the range of 'a' to
  236.                             'y' or 'D' to 'M'
  237.                 [^a-r]      matches any character not in the range of
  238.                             'a' to 'r'
  239.                 []ab]       matches either 'a', 'b' or ']'
  240.                 [-ab]       matches either 'a', 'b' or '-'
  241.                 [ab-]       matches either 'a', 'b' or '-'
  242.                 [^-ab]      matches any character but 'a', 'b' or '-'
  243.                 [\tb]       matches either 'b' or a horizontal tab
  244.                 [b\014]     matches either 'b' or 0x0B
  245.                 [*+$]       matches either '*', '+' or '$'
  246.  
  247.                 NOTE: UNIX  EGREP does  not  support  escape  characters
  248.                 inside character classes.
  249.  
  250.  
  251.             Within  a  character  class,  only  the  metacharacters  '^'
  252.             (character class  negation), '-' (character class range) and
  253.             '\' (escape character) are recognized.
  254.  
  255.             When the  "character  class  negation"  ('^')  metacharacter
  256.             appears as  the FIRST  character after  the "character class
  257.             begin" ('[')  metacharacter, it indicates that the character
  258.             class is  to be  negated.  In other words, it indicates that
  259.             the  regular   expression  character  it  represents  should
  260.             consist of any of the characters NOT in the remainder of the
  261.             character class string.
  262.  
  263.             If the  "character  class  negation"  metacharacter  appears
  264.             anywhere else  in the  character class string, it is treated
  265.             as a literal character.
  266.  
  267.             It is  often convenient  to specify  a range  of  contiguous
  268.             characters from the character set with the "character range"
  269.             metacharacter ('-').  For example,
  270.  
  271.                 "abcdefghijklmn"
  272.  
  273.             can be more clearly and easily stated as:
  274.  
  275.                 "a-n"
  276.  
  277.             If the  "character range" metacharacter is the FIRST or LAST
  278.             character in  the character  class  string,  or  immediately
  279.             follows a  "character class  negation" metacharacter,  it is
  280.             taken to be a literal character.
  281.  
  282.             Since character  ranges  are  not  the  same  for  different
  283.             characters sets  (for example,  "0-z" in  ASCII is many more
  284.             characters than  it is  in  EBCDIC),  EGREP  will  issue  an
  285.             "implementation dependent" warning message for any character
  286.             range where  the  low  and  high  characters  are  not  both
  287.             uppercase  alphabetic,   lowercase   alphabetic   or   digit
  288.             characters.
  289.  
  290.             Escape characters  have their usual meaning inside character
  291.             classes.
  292.  
  293.             Finally,  a   "character  class   end"  metacharacter  (']')
  294.             appearing  immediately   after  a  "character  class  begin"
  295.             metacharacter is  taken to be a literal character within the
  296.             character class string.  It is illegal to specify an "empty"
  297.             character class (i.e. - "[]").
  298.  
  299.  
  300.         1.8.  Grouping of Regular Expressions
  301.  
  302.             So far  we have considered only single characters in regular
  303.             expressions, where  each character follows the preceding one
  304.             in the  expression.   In the parlance of computer scientists
  305.             and linguists,  we say  that each  of  these  characters  is
  306.             "concatenated" with the preceding regular expression.
  307.  
  308.             There will  be times,  however, when we will want to specify
  309.             not single  characters but  regular expressions as part of a
  310.             regular expression.   Stepping  ahead a  bit, the  "closure"
  311.             metacharacter ('*')  allows you  to  specify  zero  or  more
  312.             occurrences of the immediately preceding regular expression.
  313.             As an example:
  314.  
  315.                 ab*
  316.  
  317.             will match "a", "ab", "abb", "abbb" and so on.
  318.  
  319.             Suppose however  that we  only want to match strings such as
  320.             "aab", "aabab", "aababab" and so on.  To do this, we use:
  321.  
  322.                 a(ab)*
  323.  
  324.             where the  string inside  the "group begin" ('(') and "group
  325.             end" (')') metacharacters is taken as a regular expression.
  326.  
  327.             Grouping is  recursive.   That is,  we can  specify  regular
  328.             expressions within  regular expressions  by means  of nested
  329.             groupings.  Thus, as an example:
  330.  
  331.                 a(b(cd)*)*
  332.  
  333.             is legal, and will match "a", "ab", "abcdbbcd" and so on.
  334.  
  335.  
  336.         1.9.  Alternation
  337.  
  338.             Suppose we  want to  specify  that  our  regular  expression
  339.             should match  either "ab"  or "cd".   If  these were  single
  340.             characters, we  could use  a character  class.  However, for
  341.             regular  expressions   we   must   use   the   "alternation"
  342.             metacharacter ('|'), such as in:
  343.  
  344.                 ab|cd
  345.  
  346.             which will match either "ab" or "cd".
  347.  
  348.             Note carefully  that the two regular expressions shown above
  349.             did not have to be grouped.  The "alternation" metacharacter
  350.             has a  "lower precedence"  than that of concatenated literal
  351.             characters.   See Section 4, "Metacharacter Precedence", for
  352.             a full explanation.
  353.  
  354.  
  355.         1.10. Optional Expressions
  356.  
  357.             The "option"  metacharacter  ('?')  specifies  zero  or  one
  358.             occurrences of  an immediately preceding regular expression.
  359.             Thus:
  360.  
  361.                 a(bc)?d?
  362.  
  363.             matches "a", "abc", "ad" and "abcd" only.
  364.  
  365.  
  366.         1.11. Repeated Expressions
  367.  
  368.             The "closure"  metacharacter ('*')  specifies zero  or  more
  369.             occurrences of  an immediately preceding regular expression.
  370.             Thus:
  371.  
  372.                 ab*
  373.  
  374.             matches "a", "ab", "abbb" and so on, while
  375.  
  376.                 a(bc)*
  377.  
  378.             matches "a", "abc", "abcbcbcbc" and so forth. Similarly,
  379.  
  380.                 [a-m]*
  381.  
  382.             matches ""  (no character),  "c", "cmdgijal"  and in general
  383.             any string that contains only letters between "a" and "m".
  384.  
  385.             Closely related  is  the  "positive  closure"  metacharacter
  386.             ('+'),  which  specifies  one  or  more  occurrences  of  an
  387.             immediately preceding regular expression.  As an example:
  388.  
  389.                 a(bc)+
  390.  
  391.             matches "abc", "abcbc", "abcbcbcbc" and so forth.
  392.  
  393.             Finally, there  is the  "iteration construct".   Suppose you
  394.             want to  search for  these string only: "abab", "ababab" and
  395.             "abababab".  You could use the regular expression:
  396.  
  397.  
  398.                 abab|ababab|abababab
  399.  
  400.             However, a simpler way to write this would be:
  401.  
  402.                 (ab){2,4}
  403.  
  404.             which simply  specifies that  the  regular  expression  will
  405.             match from two to four occurrences of the regular expression
  406.             "ab". The general format of the iteration construct is:
  407.  
  408.                 r{m,n}
  409.  
  410.             where 'r'  is a  regular expression, 'm' is the least number
  411.             of occurrences  of 'r'  that will be matched, and 'n' is the
  412.             greatest number.   The value of 'm' must be between zero and
  413.             254, while  the value  of 'n'  must be  between one and 255.
  414.             Further, 'm' must always be less than 'n'.
  415.  
  416.             A word of warning, however.  UNIX EGREP does not support the
  417.             iteration construct,  and for  a  good  reason:  EGREP  must
  418.             consume  inordinate   amounts  of  memory  in  building  its
  419.             internal state  machine tables  to represent  the  iteration
  420.             construct.   Do not  be surprised  if you see "ERROR: out of
  421.             memory" for even moderate values of 'm' and 'n'.
  422.  
  423.  
  424.         2.  Metacharacter Precedence
  425.  
  426.         2.1.  Simple Arithmetic Precedences
  427.  
  428.             You have  seen metacharacter  precedence before.   In simple
  429.             arithmetic,  the  multiplication  metacharacter  '*'  has  a
  430.             higher  precedence   than  the   addition  and   subtraction
  431.             metacharacters '+' and '-', while the division metacharacter
  432.             '/' has  a higher  precedence again.   Finally, grouping has
  433.             the highest precedence of all.
  434.  
  435.             Do you see the relationship to regular expressions?  We even
  436.             have concatenation of digits to form "numbers".
  437.  
  438.             Let's look  at an  example.   When calculating the result of
  439.             the arithmetic expression:
  440.  
  441.                 ( 7 + 3 ) * ( 8 + 12 / ( 4 - 2 ) ) - 3
  442.  
  443.             we first look for the most deeply nested group, in this case
  444.             "( 4 - 2 )".  We solve this and get:
  445.  
  446.                 ( 7 + 3 ) * ( 8 + 12 / 2 ) - 3
  447.  
  448.             We again  look for groupings, and find two at the same level
  449.             of nesting.  We solve the first one to get:
  450.  
  451.                 10 * ( 8 + 12 / 2 ) - 3
  452.  
  453.             For the  remaining group,  division  takes  precedence  over
  454.             addition, and so we get:
  455.  
  456.                 10 * ( 8 + 6 ) - 3
  457.  
  458.             and then
  459.  
  460.                 10 * 14 - 3
  461.  
  462.             We no longer have any groups to consider, but multiplication
  463.             takes precedence over subtraction, and so we get:
  464.  
  465.                 140 - 3
  466.  
  467.             and finally
  468.  
  469.                 137
  470.  
  471.             as our answer.
  472.  
  473.         2.2.  Regular Expressions
  474.  
  475.             Regular expressions  are written in exactly the same manner,
  476.             using the  precedences of  the metacharacters  recognized by
  477.             EGREP.   These are  shown in  context as follows, grouped in
  478.             DECREASING order of precedence:
  479.  
  480.                 Metachar      Function
  481.                 -----------------------
  482.  
  483.                 --------------------------------------------------------
  484.                 (r)           Grouped regular expression 'r'
  485.                 --------------------------------------------------------
  486.                 c             Literal character 'c'
  487.                 \c            Escape character 'c'
  488.                 "s"           Literal string 's'
  489.                 .             Any character
  490.                 [s]           Character class
  491.                 --------------------------------------------------------
  492.                 r?            Optional regular expression 'r'
  493.                 r*            Closure of regular expression 'r'
  494.                 r+            Positive closure of regular expression 'r'
  495.                 r{m,n}        Iteration of regular expression 'r'
  496.                 --------------------------------------------------------
  497.                 r1r2          Regular expression 'r1' followed by 'r2'
  498.                 --------------------------------------------------------
  499.                 r1|r2         Alternation of regular expressions 'r1'
  500.                               and 'r2'
  501.                 --------------------------------------------------------
  502.                 ^             Beginning of line
  503.                 $             End of line
  504.                 --------------------------------------------------------
  505.  
  506.  
  507.             Remember, you  perform the operations in order of decreasing
  508.             precedence. As  an example,  consider the  following regular
  509.             expression:
  510.  
  511.                 (ab|cd)?(ef)*
  512.  
  513.             Remembering that  each literal character can be considered a
  514.             regular expression  'r', this expression would be considered
  515.             by EGREP  in the  following manner  (where 'rn' is a regular
  516.             expression):
  517.  
  518.                 (r1r2|r3r4)?(r5r6)*
  519.  
  520.                 (r7|r8)?(r9)*
  521.  
  522.  
  523.                 r10?r9*
  524.  
  525.                 r11r12
  526.  
  527.                 r13
  528.  
  529.             with EGREP then matching such strings as "abefef", "efefef",
  530.             "cdef" and "cdcd", but not "abc", "abcd", or "abcdef".
  531.  
  532.  
  533.  
  534.  
  535.