regular expressions in python import re pattern '(england|wales|scotland)input 'england for footballwales for rugby and scotland for the highland gamesprint(re subn(pattern,'scotland'input )the output from this is('scotland for footballscotland for rugby and scotland for the highland games' the compile(function most regular expression operations are available as both module-level functions (as described aboveand as methods on compiled regular expression object the module-level functions are typically simplified or standardised ways to use the compiled regular expression in many cases these functions are sufficient but if finer grained control is required then compiled regular expression may be used re compile(patternflags= the compile(function compiles regular expression pattern into regular expression objectwhich can be used for matching using its match()search(and other methods as described below the expression' behaviour can be modified by specifying flags value the statementsprog re compile(patternresult prog match(stringare equivalent to result re match(patternstringbut using re compile(and saving the resulting regular expression object for reuse is more efficient when the expression will be used several times in single program compiled regular expression objects support the following methods and attributespattern search(stringposendposscan through string looking for the first location where this regular expression produces match and return corresponding match object return none if no position in the string