When using regex to match a string in Python, there are two steps:
Compile the regex.
Use the compiled regex to match a string.
Therefore, if a regular expression is used repeatedly, compiling it every time can be wasteful.
To avoid this, Python allows us to pre-compile a regular expression once and then reuse the compiled object for subsequent matches. This can improve performance and efficiency significantly.
The above example shows how to use the compile() function from the re module to pre-compile a regex and use it later. As long as the strings can’t match the regex, the match() function will return None.