home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / MISC / lgrep_v2.lzh / regex < prev   
Text File  |  1994-01-17  |  3KB  |  91 lines

  1. Regular expressions are studied by every Computer Science Master's
  2. student, so...
  3.  
  4.  
  5. A little bit of Computer Science.....
  6.  
  7. A regular expression is a method or notation of describing a regular
  8. language.  A language is called "regular" if there exists a finite
  9. acceptor for it.  That is to say, a finite state machine exists that
  10. describes the language. [1]
  11.  
  12. .... ok, that was fun, but what does it mean to grep.....
  13.  
  14. A regular expression is the "machine" that is "executed" against a string.
  15. The string will either "execute" TRUE or "execute" FALSE.  There is a
  16. specific notation to a regular expression.  Traditionally, grep uses the
  17. following:
  18.  
  19. ^    A circumflex at the beginning of an expression matches the
  20.      beginning of a line.
  21.  
  22. $    A dollar-sign at the end of an expression matches the end of a line.
  23.  
  24. .    A period matches any character except the newline.
  25.  
  26. *    An expression followed by an asterisk matches zero or more
  27.      occurrances of that expression.
  28.  
  29. +    An expression followed by a plus sign matches one or more
  30.      occurrances of that expression.
  31.  
  32. []   A string enclosed in square brackets matches any character in
  33.      that string, but no others.  If the first character in the
  34.      string is a circumflex, the expression matches any character
  35.      except newline and the characters in the string.  A range of
  36.      characters may be specified by two characters separated by "-"
  37.  
  38. :    A colon is always followed by another character which matches an
  39.      indicated class.  ":a" mactches any alphabetic character.  ":d"
  40.      matches any digit character.  ":n" matches any alphanumeric
  41.      character.  ": " matches spaces, tabs and other control characters,
  42.      such as newline.
  43.      
  44. \    The backslash quotes any character.
  45.  
  46. x    An ordinary character which is not mentioned above matches that
  47.      character.
  48.  
  49. The concatenation of a regular expression is another  regular expression.
  50.  
  51. So... now for some examples:
  52.  
  53. Regular expression                       String                         Result
  54.  
  55. abc                                      abcdef                          TRUE
  56.  
  57. abc$                                     abcdef                          FALSE
  58.  
  59. ..abc..                                  ababcab                         TRUE
  60.                                          xyabcay                         TRUE
  61.  
  62. .*def                                    abcdef                          TRUE
  63.  
  64. :dfoo                                    1foo                            TRUE
  65.  
  66. :d+foo                                   123foo                          TRUE
  67.                                          afoo                            FALSE
  68.  
  69. :d*foo                                   123foo                          TRUE
  70.                                          afoo                            TRUE
  71.  
  72. [abc]                                    axyz                            TRUE
  73.                                          uxyz                            FALSE
  74.  
  75. [^abc]                                   abc                             FALSE
  76.  
  77. abc\.xyz                                 abc.xyz                         TRUE
  78.                                          abcYxyz                         FALSE
  79.  
  80.  
  81. [1] An Introduction to Formal Languages and Automata, Peter Linz,
  82.     D.C. Heath and Co., ISBN: 0-669-17342-8
  83.  
  84.  
  85.  
  86. Brad Spencer
  87.  
  88. brad@anduin.eldar.org
  89. spencer@austin.onu.edu
  90.  
  91.