regular expressions text re sub( """"html_text# text re sub( "]*?>""\ \ "text# text re sub( "]*?>"""text# text re sub( "&#(\ +);"lambda mchr(int( group( )))texttext re sub( "&([ -za- ]+);"char_from_entitytext# text re sub( "\ (?:\xa \ ]+\ )+""\ "text# return re sub( "\ \ +""\ \ "text strip()# the first regexmatches html commentsincluding those with other html tags nested inside them the re sub(function replaces as many matches as it finds with the replacement--deleting the matches if the replacement is an empty stringas it is here (we can specify maximum number of matches by giving an additional integer argument at the end we are careful to use nongreedy (minimalmatching to ensure that we delete one comment for each matchif we did not do this we would delete from the start of the first comment to the end of the last comment in python the re sub(function does not accept any flags as argumentsand since means "any character except newline"we must look for or \ and we must look for these using alternation rather than character classsince inside character class has its literal meaningthat isperiod an alternative would be to begin the regex with the flag embeddedfor example(? )or we could compile regex object with the re dotall flagin which case the regex would simply be from python re split()re sub()and re subn()can all accept flags argumentso we could simply use and pass the re dotall flag the second regex]*?>matches opening paragraph tags (such as or it matches the opening < (or < )then any attributes (using nongreedy matching)and finally the closing the second call to the re sub(function uses this regex to replace opening paragraph tags with two newline characters (the standard way to delimit paragraph in plain text filethe third regex]*?>matches any tag and is used in the third re sub(call to delete all the remaining tags html entities are way of specifying non-ascii characters using ascii characters they come in two forms&namewhere name is the name of the character--for example&copyfor ( )--and &#digitswhere digits are decimal digits identifying the unicode code point--for example&# for ythe fourth call to re sub(uses the regex &#(\ +);which matches the digits form and captures the digits into capture group instead of literal replacement text we have passed lambda function when function is passed to re sub(it calls the function once for each time it matchespassing the match object as the function' sole argument inside the lambda function we retrieve the digits (as