home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine 1995 / ARCHIVE95.iso / discs / shareware / share_46 / fre / re < prev    next >
Text File  |  1992-04-23  |  3KB  |  93 lines

  1. What is a regular expression ?
  2. ==============================
  3.  
  4. It's a fancy wildcarding system - i.e.  you don't need exact matches.  A
  5. regular expression is really a mini-programming language with the
  6. following "commands":
  7.     .    any character
  8.     ^    start of line
  9.     $    end of line
  10.     \`    start of string (*)
  11.     \'    end of string
  12.     \w    word character [A-Za-z0-9]
  13.     \<    start of word
  14.     \>    end of word
  15.     \@    word boundary
  16.     [...]    set of characters (*)
  17.  
  18.     \s    first character of a symbol [a-z_A-Z]
  19.     \y    any symbol character [a-z_A-Z0-9]
  20.     \S    a whole symbol
  21.     \W    a whole word
  22.  
  23. \a, \b, \f, \n, \r, \t match the corresponding C escape character If you
  24. need to specify a code by number use the C \0..  or \x..  escape
  25. sequences.  Spaces are significant.  ANY OTHER CHARACTER MATCHES ITSELF
  26.  
  27. There are also modifiers:
  28.  
  29.     |    or, "re1|re2" match either RE1 or RE2
  30.     ~    not, "~c" matches anything but the next CHARACTER
  31.     +    many, "re+" matches RE repeated one or more times
  32.     *    repeat, "re+" matches RE repeated zero or more times
  33.     ?    optional, "re?" matches RE repeated zero or one times
  34.  
  35. Parenthesis () can be used to group REs
  36.  
  37. Finally, there are memories:
  38.  
  39.     \{    start recording
  40.     \}    end recording, and place in next memory (starts with memory 1)
  41.     \<n>    \1, ... \9 will then match the corresponding memory
  42.                           
  43. (*) strings:
  44.  strings are the same as lines in this program
  45.  
  46. (*) character sets:
  47.  character sets are used to match one of a group of characters.  All
  48. characters are taken literally except "-", "]", and "\".  The "-"
  49. indicates a range, unless it is the first or last character, or the
  50. first character after a previous range.  The "\" can be used to include
  51. a "]" or a "-" in the set, or to introduce a C escape sequence, or (if
  52. not the rhs of a range) one of the predefined character sets ("\s",
  53. "\y", or "\w"). 
  54.  
  55.  
  56.  
  57. Example regular expressions
  58. ===========================
  59.  
  60. For Risc-Os users unaquainted with REs, note that the standard filename
  61. wildcarding can be done if you substitute "." for "#", and ".*" for "*"!
  62.  
  63. The directory specification "$.doc##.monday*" would translate to the RE:
  64.     "\$\.doc..\.monday.*"
  65.          ^^ ^   ^         ^^
  66.       | |   \         ++--> "*" translates to ".*"
  67.           | |    \
  68.           | \     +-----------> "#" translates to "."
  69.           |  \
  70.           |   +---------------> "." is a reserved character
  71.           \
  72.            +------------------> "$" is a reserved character
  73.  
  74. To find either of the words "has", "have", or "had":
  75.         "\<(has|have|had)\>"
  76.           ^               ^
  77.           +---------------+---> These ensure don't match "hasty", "haddock",
  78.                 or "chastity", etc...
  79.  
  80. Look for symbols ending with a digit
  81. ------------------------------------
  82.  
  83.     "\y[0-9]\╗"
  84.  
  85. Find a symbol used in two places on a line
  86. ------------------------------------------
  87.  
  88. 1.    "\{\S\}.*\1"
  89.  
  90.   Almost.  But fails on "Zappa = Frank_Zappa".
  91.  
  92. 2.    "\{\S\}.*\½\1\╗"
  93.