7.32. Regex RE Compile

  • re.compile()

  • Used when pattern is reused (especially in the loop)

Prepare:

>>> x = re.compile(pattern)

Usage:

>>> x.findall(string)
>>> x.match(string)
>>> x.search(string)

7.32.1. SetUp

>>> import re
>>> DATA = [
...     'alice@example.com',
...     'bob@example.com',
...     'carol@example.com',
...     'dave@example.org',
...     'eve@example.org',
...     'mallory@example.net',
... ]

7.32.2. Without Compilation

  • Python will compile pattern during every loop iteration

  • After compilation it will perform matching

Compiles at every loop iteration, and then matches:

>>> valid = r'^[a-z]+@example.com$'
>>>
>>> for email in DATA:
...     if re.match(valid, email):
...         print(f'valid: {email}')
...     else:
...         print(f'error: {email}')
...
valid: alice@example.com
valid: bob@example.com
valid: carol@example.com
error: dave@example.org
error: eve@example.org
error: mallory@example.net

7.32.3. With Compilation

  • Python will compile pattern once, before loop

  • Then in the loop, only matching is performed

Compiling before loop, hence matching only inside:

>>> valid = re.compile(r'^[a-z]+@example.com$')
>>>
>>> for email in DATA:
...     if valid.match(email):
...         print(f'valid: {email}')
...     else:
...         print(f'error: {email}')
...
valid: alice@example.com
valid: bob@example.com
valid: carol@example.com
error: dave@example.org
error: eve@example.org
error: mallory@example.net