Regexp Over Multiple Lines

Keith vetter at lincom-asg.com
Wed Sep 4 17:40:22 CEST 2002


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