Regexp Over Multiple Lines
Brandon Beck
bbeck at REMOVE-THIS-TO-EMAIL-ME.austin.rr.com
Wed Sep 4 11:59:03 EDT 2002
Yes, please see the python online documentation for the re module
(http://www.python.org/doc/current/lib/module-re.html) the section you
specifically want is at
(http://www.python.org/doc/current/lib/node99.html). You'll want to
pass the VERBOSE flag to your compilation statement.
pattern = re.compile('''
<first line> # comment
...
<last line> # comment
''', re.VERBOSE);
Additionally you may want to check out the regular expression howto at
sourceforge (http://py-howto.sourceforge.net/regex/regex.html).
Keith wrote:
> In Python can I have a regular expression that spans multiple lines...
> e.g. In Perl I may define a regular expression like:
>
> $depends_def = qr/
> [\w_\.]+\s*: # simobject:
> \s*
> [\w_\.]+ # job name
> \(\s*(?:[\d]*)?\s*\) # arg list for job (optional
> num)
> \s+
> depends\s+on # keywords "depends on"
> \s+
> (?:
> [\w_\.]+\s*: # simobject:
> \s*
> (?:
> [\w_\.]+ # job name
> \(\s*(?:[\d]*)?\s*\) # arg list for job (optional
> num)
> (?:
> (?:
> \s+
> and\s*[\w_\.]*\s*: # "and simobject:"
> \s*
> [\w_]* # job name
> \(\s*(?:[\d]*)?\s*\) # arg list for job (optional
> num)
> )+
> )? # match optional "and" 1 or
> more times
> )
> )
> /sx;
>
> then later I can use that regular expression in another regular
> expression.
>
> It helps to span multiple lines for commenting what the heck the ugly
> regular expression is trying to do...
>
> BTW: I am investigating Python and have no knowledge of it. Our Perl
> code has just gotten out-of-hand.
>
> Thanks,
> Keith
More information about the Python-list
mailing list