none
[openafs-wiki.git] / TWiki / RegularExpression.mdwn
1 Regular expressions allow more specific queries then a simple query.
2
3 **Examples**
4
5 <table>
6   <tr>
7     <td> compan(y|ies) </td>
8     <td> Search for <em>company</em> , <em>companies</em></td>
9   </tr>
10   <tr>
11     <td> (peter|paul) </td>
12     <td> Search for <em>peter</em> , <em>paul</em></td>
13   </tr>
14   <tr>
15     <td> bug* </td>
16     <td> Search for <em>bug</em> , <em>bugs</em> , <em>bugfix</em></td>
17   </tr>
18   <tr>
19     <td> [Bb]ag </td>
20     <td> Search for <em>Bag</em> , <em>bag</em></td>
21   </tr>
22   <tr>
23     <td> b[aiueo]g </td>
24     <td> Second letter is a vowel. Matches <em>bag</em> , <em>bug</em> , <em>big</em></td>
25   </tr>
26   <tr>
27     <td> b.g </td>
28     <td> Second letter is any letter. Matches also <em>b&amp;g</em></td>
29   </tr>
30   <tr>
31     <td> [a-zA-Z] </td>
32     <td> Matches any one letter (not a number and a symbol) </td>
33   </tr>
34   <tr>
35     <td> [^0-9a-zA-Z] </td>
36     <td> Matches any symbol (not a number or a letter) </td>
37   </tr>
38   <tr>
39     <td> [A-Z][A-Z]* </td>
40     <td> Matches one or more uppercase letters </td>
41   </tr>
42   <tr>
43     <td> [0-9][0-9][0-9]-[0-9][0-9]- <br /> [0-9][0-9][0-9][0-9] </td>
44     <td valign="top"> US social security number, e.g. 123-45-6789 </td>
45   </tr>
46 </table>
47
48 Here is stuff for our UNIX freaks: <br /> (copied from 'man grep')
49
50           \c    A backslash (\) followed by any special character is  a
51                          one-character  regular expression that matches the spe-
52                          cial character itself.  The special characters are:
53
54                                         +        `.', `*', `[',  and  `\'  (period,  asterisk,
55                                                   left  square  bracket, and backslash, respec-
56                                                   tively), which  are  always  special,  except
57                                                   when they appear within square brackets ([]).
58
59                                         +        `^' (caret or circumflex), which  is  special
60                                                   at the beginning of an entire regular expres-
61                                                   sion, or when it immediately follows the left
62                                                   of a pair of square brackets ([]).
63
64                                         +        $ (currency symbol), which is special at  the
65                                                   end of an entire regular expression.
66
67           .      A `.' (period) is a  one-character  regular  expression
68                          that matches any character except NEWLINE.
69
70           [string]
71                          A non-empty string of  characters  enclosed  in  square
72                          brackets  is  a  one-character  regular expression that
73                          matches any one character in that string.  If, however,
74                          the  first  character of the string is a `^' (a circum-
75                          flex or caret), the  one-character  regular  expression
76                          matches  any character except NEWLINE and the remaining
77                          characters in the string.  The  `^'  has  this  special
78                          meaning only if it occurs first in the string.  The `-'
79                          (minus) may be used to indicate a range of  consecutive
80                          ASCII  characters;  for example, [0-9] is equivalent to
81                          [0123456789].  The `-' loses this special meaning if it
82                          occurs  first (after an initial `^', if any) or last in
83                          the string.  The `]' (right square  bracket)  does  not
84                          terminate  such a string when it is the first character
85                          within it (after an initial  `^',  if  any);  that  is,
86                          []a-f]  matches either `]' (a right square bracket ) or
87                          one of the letters a through  f  inclusive.    The  four
88                          characters  `.', `*', `[', and `\' stand for themselves
89                          within such a string of characters.
90
91           The following rules may be used to construct regular expres-
92           sions:
93
94           *      A one-character regular expression followed by `*'  (an
95                          asterisk)  is a regular expression that matches zero or
96                          more occurrences of the one-character  regular  expres-
97                          sion.  If  there  is  any choice, the longest leftmost
98                          string that permits a match is chosen.
99
100           ^      A circumflex or caret (^) at the beginning of an entire
101                          regular  expression  constrains that regular expression
102                          to match an initial segment of a line.
103
104           $      A currency symbol ($) at the end of an  entire  regular
105                          expression  constrains that regular expression to match
106                          a final segment of a line.
107
108           *      A  regular  expression  (not  just     a       one-
109                          character regular expression) followed by `*'
110                          (an asterisk) is a  regular  expression  that
111                          matches  zero or more occurrences of the one-
112                          character regular expression.  If  there  is
113                          any  choice, the longest leftmost string that
114                          permits a match is chosen.
115
116           +      A regular expression followed by `+' (a  plus
117                          sign)  is  a  regular expression that matches
118                          one or more occurrences of the  one-character
119                          regular  expression.  If there is any choice,
120                          the longest leftmost string  that  permits  a
121                          match is chosen.
122
123           ?      A regular expression followed by `?' (a ques-
124                          tion  mark)  is  a  regular  expression  that
125                          matches zero or one occurrences of  the  one-
126                          character  regular  expression.        If there is
127                          any choice, the longest leftmost string  that
128                          permits a match is chosen.
129
130           |      Alternation:    two     regular         expressions
131                          separated  by  `|'  or NEWLINE match either a
132                          match for  the  first  or  a  match  for  the
133                          second.
134
135           ()    A regular expression enclosed in  parentheses
136                          matches a match for the regular expression.
137
138           The order of precedence of operators at the same parenthesis
139           level  is  `[ ]'  (character  classes),  then  `*'  `+'  `?'
140           (closures),then  concatenation,  then  `|'  (alternation)and
141           NEWLINE.