FAQ
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

Search Discussions

  • Gabriel Genellina at Feb 14, 2007 at 6:27 am
    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

    --
    Gabriel Genellina
  • John Nagle at Feb 14, 2007 at 7:11 am

    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

    From http Wed Feb 14 07:44:02 2007
    From: http (Paul Rubin)
    Date: 13 Feb 2007 22:44:02 -0800
    Subject: threading and multicores, pros and cons
    References: <mailman.3976.1171427985.32031.python-list@python.org>
    <7xodnx2vir.fsf@ruckus.brouhaha.com>
    <mailman.3984.1171434807.32031.python-list@python.org>
    Message-ID: <7x7iulqlvx.fsf@ruckus.brouhaha.com>

    Maric Michaud <maric at aristote.info> writes:
    Le mercredi 14 f?vrier 2007 05:49, Paul Rubin a ?crit?:
    Basically Python applications are usually not too CPU-intensive; there
    are some ways you can get parallelism with reasonable extra effort;
    Basically, while not CPU intensive, application server needs to get
    benefit of all resources of the hardware.
    But this is impossible--if the application is not CPU intensive, by
    definition it leaves a lot of the available CPU cycles unused.
    When a customer comes with his new beautiful dual-core server and
    get a basic plone install up and running, he will immediately
    compare it to J2EE and wonder why he should pay a consultant to make
    it work properly. At this time, it 's not easy to explain him that
    python is not flawed compared to Java, and that he will not regret
    his choice in the future. First impression may be decisive.
    That is true, parallelism is an area where Java is ahead of us.
    The historical explanation should be inefficient here, I'm
    afraid. What about the argument that said that multi threading is
    not so good for parallelism ? Is it strong enough ?
    It's not much good for parallelism in the typical application that
    spends most of its time blocked waiting for I/O. That is many
    applications. It might even even be most applications. But there are
    still such things as CPU-intensive applications which can benefit from
    parallelism, and Python has a weak spot there.
  • Gabriel Genellina at Feb 14, 2007 at 6:58 am
    En Wed, 14 Feb 2007 04:11:37 -0300, John Nagle <nagle at animats.com>
    escribi?:
    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
    }))
    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.

    --
    Gabriel Genellina

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 14, '07 at 4:07a
activeFeb 14, '07 at 7:11a
posts4
users2
websitepython.org

2 users in discussion

John Nagle: 2 posts Gabriel Genellina: 2 posts

People

Translate

site design / logo © 2022 Grokbase