How can this Perl regular expression be expressed in Python?

J

John Nagle

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?

John Nagle
 
G

Gabriel Genellina

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
 
G

Gabriel Genellina

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

From the context it appears to be the "last matched group", or something
like that... but best look for some authoritative answer.
 
J

John Nagle

Gabriel said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top