Regexp Over Multiple Lines

Keith vetter at lincom-asg.com
Wed Sep 11 09:29:43 EDT 2002


vetter at lincom-asg.com (Keith) wrote in message news:<565e316f.0209040740.36573b56 at posting.google.com>...
> 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

After reading the links you guys suggested... it looks like the python
regular expressions are very powerful.

I am sure that this has been discussed... but in a nutshell can you
tell me where python lacks in power behind perl.  If I go to rewrite
my Perl code, which has gotten really messy, in Python will I be
saying over and over again, "Man, I can't do =that= in Python!"

I am testing the waters before I dive in!

Take care,
Keith



More information about the Python-list mailing list