and fourth tests shows how alternatives can be grouped by both position and nameand the last test matches #define lines--more on this pattern in momentc:\pp \langpython re-groups py (' '' '' '(' '' '' '{' '' '' '' '' '' '('spam'' 'finallybesides matches and substring extractionre also includes tools for string replacement or substitution (see example - example - pp \lang\re-subst py "substitutionsreplace occurrences of pattern in stringimport re print(re sub('[abc]''*''xaxaxbxbxcxc')print(re sub('[abc] ''*''xa-xa_xb-xb_xc-xc_')alternatives char print(re sub('spam''spam\\ 'group back ref (or ''' spamy spam')def mapper(matchobj)return 'spammatchobj group( print(re sub('spam'mapper' spamy spam')mapping function in the first testall characters in the set are replacedin the secondthey must be followed by an underscore the last two tests illustrate more advanced group back-references and mapping functions in the replacement note the \\ required to escape \ for python' string rulesr'spam\ would work just as well see also the earlier interactive tests in the section for additional substitution and splitting examplesc:\pp \langpython re-subst py * * * * *xxa- *xb- *xc-xspamxspamy spamxspamy scanning header files for patterns to wrap uplet' turn to more realistic examplethe script in example - puts these pattern operators to more practical use it uses regular expressions to find #define and #include lines in header files and extract their components the generality of the patterns makes them detect variety of line formatspattern groups (the parts in parenthesesare used to extract matched substrings from line after match regular expression pattern matching