Regular expressions are compiled into RegexObject
instances, which then have methods for various operations such
as searching for pattern matches or
performing string substitutions.
re.compile() also accepts an optional flags
argument, used to enable various special features and syntax
variations. We'll go over the available settings later, but for now a
single example will do:
>>> import re
>>> p = re.compile('ab*')
>>> print p
<re.RegexObject instance at 80b4150>
The RE is passed to re.compile() as a string.
REs are handled as strings because regular expressions aren't
part of the core Python language, and no special syntax was created for
expressing them. (There are applications that don't need REs at all,
so there's no need to bloat the language specification by including
them.) Instead, the re module is just a C extension module,
just like the string module.
>>> p = re.compile('ab*', re.IGNORECASE)
Putting REs in strings keeps the Python language simpler, but has one
disadvantage which is the topic of the next section.