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 oneYield pauses a function."next() resumes where it left off.The for loop will automatically call the next() function to get values from the fib() generator and assign them to the for loop index variable (n).Pass a generator to the list() function, and it will iterate through the entire generator (just like the for 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, but r'\t' is really the backslash character \ followed by the letter t. 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