How can this Perl regular expression be expressed in Python?

John Nagle nagle at animats.com
Wed Feb 14 02:11:37 EST 2007


Gabriel Genellina wrote:
> En Wed, 14 Feb 2007 01:07:33 -0300, John Nagle <nagle at animats.com>  
> escribió:
> 
>> Here's a large Perl regular expression, from a Perl address parser in  
>> CPAN:
>>
>>     use re 'eval';
>>      $Addr_Match{street} = qr/
>>          (?:
>>            # special case for addresses like 100 South Street
>>            (?:($Addr_Match{direct})\W+           (?{ $_{street} = $^N })
>>               ($Addr_Match{type})\b              (?{ $_{type}   = $^N }))
>>            |
>>            (?:($Addr_Match{direct})\W+        (?{ $_{prefix} = $^N }))?
>>            (?:
>>              ([^,]+)                (?{ $_{street} = $^N })
>>              (?:[^\w,]+($Addr_Match{type})\b    (?{ $_{type}   = $^N }))
>>              (?:[^\w,]+($Addr_Match{direct})\b    (?{ $_{suffix} = $^N 
>> }))?
>>             |
>>              ([^,]*\d)                (?{ $_{street} = $^N })
>>              ($Addr_Match{direct})\b        (?{ $_{suffix} = $^N })
>>             |
>>              ([^,]+?)                (?{ $_{street} = $^N })
>>              (?:[^\w,]+($Addr_Match{type})\b    (?{ $_{type}   = $^N }))?
>>              (?:[^\w,]+($Addr_Match{direct})\b    (?{ $_{suffix} = $^N 
>> }))?
>>            )
>>          )
>>     /ix;
>>
>> I'm trying to convert this to Python.
>>
>> Those entries like "$(Addr_Match{direct}) are other regular expressions,
>> being used here as subexpressions.  Those have already been converted
>> to forms like "Addr_Match.direct" in Python.  But how to call them?
>> Is that possible in Python, and if so, where is it documented?
> 
> 
> That would be string interpolation, like this:
> 
> Addr_Match = {"direct": "some_re_string",
>               "type": "other_re"
>              }
> 
> regexp = "%(direct)s %(type)s" % Addr_Match

    You're right.  I looked at the Perl code, and the strings are just being
inserted, not precompiled as regular expressions and called.

    Incidentally, does anybody know what "$^N" means in Perl?  That
abbreviation isn't in the list of special variables.

					John Nagle



More information about the Python-list mailing list