- Compile a regular expression pattern into a regular expression object, which can be used for matching using its
match()
,search()
and other methods, described below.
prog = re.compile(pattern)
result = prog.match(string)
is equivalent to
result = re.match(pattern,string)
but using re.compile()
and saving the resulting regular expression object for reuse is more efficient when the expression will be used several times in a single program.
re.sub
replaces all of the matches, not just the first one- Yield pauses a function."next() resumes where it left off.
- The
for
loop will automatically call thenext()
function to get values from thefib()
generator and assign them to thefor
loop index variable (n). - Pass a generator to the
list()
function, and it will iterate through the entire generator (just like thefor
loop in the previous example) and return a list of all the values. - It’s time to move up to regular expressions. In Python, all functionality related to regular expressions is contained in the
re
module. - To work around the backslash plague, you can use what is called a raw string, by prefixing the string with the letter
r
. This tells Python that nothing in this string should be escaped;'\t'
is a tab character, butr'\t'
is really the backslash character\
followed by the lettert
. I recommend always using raw strings when dealing with regular expressions; otherwise, things get too confusing too quickly (and regular expressions are confusing enough already). \b
, which means “a word boundary must occur right here- The
$
means “end of the string.” (There is a corresponding character, the caret^
, which means “beginning of the string