Using re.VERBOSE, and re-using components of regex?
MRAB
python at mrabarnett.plus.com
Tue Apr 16 20:16:14 EDT 2013
On 17/04/2013 00:45, Victor Hooi wrote:
> Hi,
>
> I'm trying to compile a regex Python with the re.VERBOSE flag (so that I can add some friendly comments).
>
> However, the issue is, I normally use constants to define re-usable bits of the regex - however, these doesn't get interpreted inside the triple quotes.
>
> For example:
>
> import re
>
> TIMESTAMP = r'(?P<timestamp>\d{2}:\d{2}:\d{2}.\d{9})'
> SPACE = r' '
> FOO = r'some_regex'
> BAR = r'some_regex'
>
> regexes = {
> 'data_sent': re.compile("""
> TIMESTAMP # Timestamp of our log message
> SPACE
> FOO # Some comment
> SPACE
> """, re.VERBOSE),
> 'data_received': re.compile("""
> TIMESTAMP # Timestamp of our log message
> SPACE
> BAR # Some comment
> SPACE
> """, re.VERBOSE),
> }
>
> Is there a way to use CONSTANTS (or at least re-use fragments of my regex), and also use re.VERBOSE so I can comment my regex?
>
You could do it like this:
import re
constants = {}
constants['TIMESTAMP'] = r'(?P<timestamp>\d{2}:\d{2}:\d{2}.\d{9})'
constants['SPACE'] = r'\ ' # Escape the space because it'll be in a
VERBOSE regex.
constants['FOO'] = r'some_regex'
constants['BAR'] = r'some_regex'
regexes = {
'data_sent': re.compile("""
{TIMESTAMP} # Timestamp of our
log message
{SPACE}
{FOO} # Some comment
{SPACE}
""".format(**constants), re.VERBOSE),
'data_received': re.compile("""
{TIMESTAMP} # Timestamp of our
log message
{SPACE}
{BAR} # Some comment
{SPACE}
""".format(**constants), re.VERBOSE),
}
More information about the Python-list
mailing list