Compiles a regular expression pattern into a regular expression object. A regex object can then be used with “match” and “search” methods.
The use of regex object is similar to “re.search” function. For example, the following 2 contructs serve the same purpose.
result = re.search(myPattern, myStr)
regexObj = re.compile(myPattern) result = regexObj.search(myStr)
If the same regex pattern needs to be used to match different texts, first use “compile” to create a regex object. This avoids compiling the same regex multiple times. Compiled pattern can also be used in functions. For example, the following is also equivalent to the above:
result = re.search(re.compile(myPattern), myStr)
The “compile” function takes a optional second argument that modifies the meaning of the given pattern. See: Python Regex Flags.
Compiled regular expression objects support the following methods and attributes. These are simply documented below. For a more detail documentation, see their functional equivalent at Regex Functions.
The following are methods for a compiled pattern object.
re.compile(pat).search(str) is equivalent to re.search(pat, str).
If ‹pattern› matches (parts of) ‹string›, then a MatchObject is returned. Returns None if pattern is not found in the string.
See “re.search” at Regex Functions for detail.
Perform match starting from position ‹m› of string.
myRegexObj.match('abcdef', 3)
is equivalent to
myRegexObj.match('abcdef'[3:])
Perfom match on a substring from index ‹startpos› to ‹endpos›.
myRegexObj.match(myString, m, n)
is equivalent to
myRegexObj.match(myString[m:n])
re.compile(pat).match(str) is equivalent to re.match(pat, str)
same meaning as the “search” method.
re.compile(pat).split(str) is equivalent to re.split(pat, str)
Perform spliting ‹n› steps. Example:
import re
myRegexObj=re.compile(r',');
mySplitResult=myRegexObj.split("a,b,c,d,e",3)
print mySplitResult # ⇒ ['a', 'b', 'c', 'd,e']
A variant syntax for split(‹string›, ‹n›).
re.compile(pat).findall(str) is equivalent to re.findall(pat, str)
re.compile(pat).finditer(str) is equivalent to re.finditer(pat, str)
re.compile(pat).sub(str) is equivalent to re.sub(pat, str)
re.compile(pat).subn(str) is equivalent to re.subn(pat, str)
The following are constants assigned by the module when a pattern object is compiled.
The flags argument used when the regex object was compiled, or
0 if no flags were provided.
Example:
import re patObj = re.compile(ur'\w', re.M|re.U) print patObj.flags # prints 40
NOTE TO DOC WRITER: needs a brief explanation here on the meaning of the returned number, or, how to turn this number into the flags.
A dictionary mapping any symbolic group names defined by
(?P<‹id›>) to group numbers. The dictionary is empty if no
symbolic groups were used in the pattern.
Example:
#-*- coding: utf-8 -*- import re myText='<img src="some.jpg" alt="beauty" width="123" height="456">' patObj = re.compile(r'src="(?P<filename>[^"]+)" alt="(?P<alttext>[^"]+)" width="(?P<width>\d+)" height="(?P<height>\d+)"') print patObj.groupindex # prints: {'height': 4, 'width': 3, 'alttext': 2, 'filename': 1}
The pattern string from which the regex object was compiled.
blog comments powered by Disqus