A List Of Patterns
Discover how to use closures to build dynamic match and apply functions that handle pluralization rules. Explore how regular expressions and function mapping simplify complex string manipulations in Python.
We'll cover the following...
Defining separate named functions for each match and apply rule isn’t really necessary. You never call them directly; you add them to the rules sequence and call them through there. Furthermore, each function follows one of two patterns. All the match functions call re.search(), and all the apply functions call re.sub(). Let’s factor out the patterns so that defining new rules can be easier.
① build_match_and_apply_functions() is a function that builds other functions dynamically. It takes pattern, search and replace, then defines a matches_rule() function which calls re.search() with the ...