File: Regexp.txt

package info (click to toggle)
grc 1.0.8
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 208 kB
  • ctags: 90
  • sloc: python: 285; makefile: 39; sh: 35
file content (187 lines) | stat: -rw-r--r-- 11,312 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
   
Regular Expression Syntax (taken from python library reference)

   A regular expression (or RE) specifies a set of strings that matches
   it; the functions in this module let you check if a particular string
   matches a given regular expression (or if a given regular expression
   matches a particular string, which comes down to the same thing).
   
   Regular expressions can be concatenated to form new regular
   expressions; if A and B are both regular expressions, then AB is also
   an regular expression. If a string p matches A and another string q
   matches B, the string pq will match AB. Thus, complex expressions can
   easily be constructed from simpler primitive expressions like the
   ones described here. For details of the theory and implementation of
   regular expressions, consult the Friedl book referenced below, or
   almost any textbook about compiler construction.
   
   A brief explanation of the format of regular expressions follows. For
   further information and a gentler presentation, consult the Regular
   Expression HOWTO, accessible from http://www.python.org/doc/howto/.
   
   Regular expressions can contain both special and ordinary characters.
   Most ordinary characters, like "A", "a", or "0", are the simplest
   regular expressions; they simply match themselves. You can
   concatenate ordinary characters, so last matches the string 'last'.
   (In the rest of this section, we'll write RE's in this special style,
   usually without quotes, and strings to be matched 'in single
   quotes'.)
   
   Some characters, like "|" or "(", are special. Special characters
   either stand for classes of ordinary characters, or affect how the
   regular expressions around them are interpreted.
   
   The special characters are:
   
   "."
          (Dot.) In the default mode, this matches any character except a newline. If the
          DOTALL flag has been specified, this matches any character including a newline.
   "^"
          (Caret.) Matches the start of the string, and in MULTILINE mode also matches
          immediately after each newline.
   "$"
          Matches the end of the string, and in MULTILINE mode also matches before a newline.
          foo matches both 'foo' and 'foobar', while the regular expression foo$ matches only
          'foo'.
   "*"
          Causes the resulting RE to match 0 or more repetitions of the preceding RE, as many
          repetitions as are possible. ab* will match 'a', 'ab', or 'a' followed by any
          number of 'b's.
   "+"
          Causes the resulting RE to match 1 or more repetitions of the preceding RE. ab+
          will match 'a' followed by any non-zero number of 'b's; it will not match just 'a'.
   "?"
          Causes the resulting RE to match 0 or 1 repetitions of the preceding RE. ab? will
          match either 'a' or 'ab'.
   *?, +?, ??
          The "*", "+", and "?" qualifiers are all greedy; they match as much text as
          possible. Sometimes this behaviour isn't desired; if the RE <.*> is matched against
          '<H1>title</H1>', it will match the entire string, and not just '<H1>'. Adding "?"
          after the qualifier makes it perform the match in non-greedy or minimal fashion; as
          few characters as possible will be matched. Using .*? in the previous expression
          will match only '<H1>'.
   {m,n}
          Causes the resulting RE to match from m to n repetitions of the preceding RE,
          attempting to match as many repetitions as possible. For example, a{3,5} will match
          from 3 to 5 "a" characters. Omitting n specifies an infinite upper bound; you can't
          omit m.
   {m,n}?
          Causes the resulting RE to match from m to n repetitions of the preceding RE,
          attempting to match as few repetitions as possible. This is the non-greedy version
          of the previous qualifier. For example, on the 6-character string 'aaaaaa', a{3,5}
          will match 5 "a" characters, while a{3,5}? will only match 3 characters.
   "\"
          Either escapes special characters (permitting you to match characters like "*",
          "?", and so forth), or signals a special sequence; special sequences are discussed
          below.
          If you're not using a raw string to express the pattern, remember that Python also
          uses the backslash as an escape sequence in string literals; if the escape sequence
          isn't recognized by Python's parser, the backslash and subsequent character are
          included in the resulting string. However, if Python would recognize the resulting
          sequence, the backslash should be repeated twice. This is complicated and hard to
          understand, so it's highly recommended that you use raw strings for all but the
          simplest expressions.
   []
          Used to indicate a set of characters. Characters can be listed individually, or a
          range of characters can be indicated by giving two characters and separating them
          by a "-". Special characters are not active inside sets. For example, [akm$] will
          match any of the characters "a", "k", "m", or "$"; [a-z] will match any lowercase
          letter, and [a-zA-Z0-9] matches any letter or digit. Character classes such as \w
          or \S(defined below) are also acceptable inside a range. If you want to include a
          "]" or a "-" inside a set, precede it with a backslash, or place it as the first
          character. The pattern []] will match ']', for example.
          You can match the characters not within a range by complementing the set. This is
          indicated by including a "^" as the first character of the set; "^" elsewhere will
          simply match the "^" character. For example, [^5] will match any character except
          "5".
   "|"
          A|B, where A and B can be arbitrary REs, creates a regular expression that will
          match either A or B. This can be used inside groups (see below) as well. To match a
          literal "|", use \|, or enclose it inside a character class, as in [|].
   (...)
          Matches whatever regular expression is inside the parentheses, and indicates the
          start and end of a group; the contents of a group can be retrieved after a match
          has been performed, and can be matched later in the string with the \number special
          sequence, described below. To match the literals "(" or "')", use \( or \), or
          enclose them inside a character class: [(] [)].
   (?...)
          This is an extension notation (a "?" following a "(" is not meaningful otherwise).
          The first character after the "?" determines what the meaning and further syntax of
          the construct is. Extensions usually do not create a new group; (?P<name>...) is
          the only exception to this rule. Following are the currently supported extensions.
   (?iLmsx)
          (One or more letters from the set "i", "L", "m", "s", "x".) The group matches the
          empty string; the letters set the corresponding flags (re.I, re.L, re.M, re.S,
          re.X) for the entire regular expression. This is useful if you wish to include the
          flags as part of the regular expression, instead of passing a flag argument to the
          compile() function.
   (?:...)
          A non-grouping version of regular parentheses. Matches whatever regular expression
          is inside the parentheses, but the substring matched by the group cannot be
          retrieved after performing a match or referenced later in the pattern.
   (?P<name>...)
          Similar to regular parentheses, but the substring matched by the group is
          accessible via the symbolic group name name. Group names must be valid Python
          identifiers. A symbolic group is also a numbered group, just as if the group were
          not named. So the group named 'id' in the example above can also be referenced as
          the numbered group 1.
          For example, if the pattern is (?P<id>[a-zA-Z_]\w*), the group can be referenced by
          its name in arguments to methods of match objects, such as m.group('id')or
          m.end('id'), and also by name in pattern text (e.g. (?P=id)) and replacement text
          (e.g. \g<id>).
   (?P=name)
          Matches whatever text was matched by the earlier group named name.
   (?#...)
          A comment; the contents of the parentheses are simply ignored.
   (?=...)
          Matches if ... matches next, but doesn't consume any of the string. This is called
          a lookahead assertion. For example, Isaac (?=Asimov) will match 'Isaac ' only if
          it's followed by 'Asimov'.
   (?!...)
          Matches if ... doesn't match next. This is a negative lookahead assertion. For
          example, Isaac (?!Asimov) will match 'Isaac ' only if it's not followed by
          'Asimov'.
          
   The special sequences consist of "\" and a character from the list below. If the ordinary
   character is not on the list, then the resulting RE will match the second character. For
   example, \$ matches the character "$".
   
   \number
          Matches the contents of the group of the same number. Groups are numbered starting
          from 1. For example, (.+) \1 matches 'the the' or '55 55', but not 'the end' (note
          the space after the group). This special sequence can only be used to match one of
          the first 99 groups. If the first digit of number is 0, or number is 3 octal digits
          long, it will not be interpreted as a group match, but as the character with octal
          value number. Inside the "[" and "]" of a character class, all numeric escapes are
          treated as characters.
   \A
          Matches only at the start of the string.
   \b
          Matches the empty string, but only at the beginning or end of a word. A word is
          defined as a sequence of alphanumeric characters, so the end of a word is indicated
          by whitespace or a non-alphanumeric character. Inside a character range, \b
          represents the backspace character, for compatibility with Python's string
          literals.
   \B
          Matches the empty string, but only when it is not at the beginning or end of a
          word.
   \d
          Matches any decimal digit; this is equivalent to the set [0-9].
   \D
          Matches any non-digit character; this is equivalent to the set [^0-9].
   \s
          Matches any whitespace character; this is equivalent to the set [ \t\n\r\f\v].
   \S
          Matches any non-whitespace character; this is equivalent to the set [^ \t\n\r\f\v].
   \w
          When the LOCALE flag is not specified, matches any alphanumeric character; this is
          equivalent to the set [a-zA-Z0-9_]. With LOCALE, it will match the set [0-9_] plus
          whatever characters are defined as letters for the current locale.
   \W
          When the LOCALE flag is not specified, matches any non-alphanumeric character; this
          is equivalent to the set [^a-zA-Z0-9_]. With LOCALE, it will match any character
          not in the set [0-9_], and not defined as a letter for the current locale.
   \Z
          Matches only at the end of the string.
   \\
          Matches a literal backslash.