write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.

Discussion in 'Python' started by iMath, Sep 29, 2012.

  1. iMath

    iMath Guest

    write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.
    iMath, Sep 29, 2012
    #1
    1. Advertising

  2. iMath

    Tim Chase Guest

    Re: write a regex matches 800-555-1212, 555-1212, and also (800)555-1212.

    On 09/28/12 19:31, iMath wrote:
    > write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.


    Okay, that was pretty easy. Thanks for the challenge :)

    -tkc
    Tim Chase, Sep 29, 2012
    #2
    1. Advertising

  3. iMath

    Paul Rubin Guest

    iMath <> writes:
    > write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.


    And then you have two problems.
    Paul Rubin, Sep 29, 2012
    #3
  4. Re: write a regex matches 800-555-1212, 555-1212, and also (800)555-1212.

    On 29/09/2012 02:35, Tim Chase wrote:
    > On 09/28/12 19:31, iMath wrote:
    >> write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.

    >
    > Okay, that was pretty easy. Thanks for the challenge :)
    >
    > -tkc
    >


    What's the run time speed like? How much memory does it use? Shouldn't
    you be using the regex module from pypi instead of the standard library
    re? Guess who's borrowed the time machine?

    --
    Cheers.

    Mark Lawrence.
    Mark Lawrence, Sep 29, 2012
    #4
  5. iMath

    Tim Chase Guest

    Re: write a regex matches 800-555-1212, 555-1212, and also (800)555-1212.

    On 09/28/12 20:58, Mark Lawrence wrote:
    > On 29/09/2012 02:35, Tim Chase wrote:
    >> On 09/28/12 19:31, iMath wrote:
    >>> write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.

    >>
    >> Okay, that was pretty easy. Thanks for the challenge :)

    >
    > What's the run time speed like?


    O(1)

    r = re.compile(
    "800-555-1212|"
    "555-1212|"
    r"\(800\) 555-1212"
    )

    (okay, so I also have one that solves the OP's underqualified
    problem, but without the OP at least *trying* to code up an answer
    and asking for help with it, I've give the snarky solution :)

    > How much memory does it use?


    Insignificant.

    > Shouldn't you be using the regex module from pypi instead of the
    > standard library re?


    Only if the OP requested it ;-)

    > Guess who's borrowed the time machine?


    Neutrino!

    -tkc
    Tim Chase, Sep 29, 2012
    #5
  6. iMath

    Ian Kelly Guest

    Re: write a regex matches 800-555-1212, 555-1212,and also (800) 555-1212.

    On Fri, Sep 28, 2012 at 8:17 PM, Tim Chase
    <> wrote:
    > On 09/28/12 20:58, Mark Lawrence wrote:
    >> On 29/09/2012 02:35, Tim Chase wrote:
    >>> On 09/28/12 19:31, iMath wrote:
    >>>> write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.
    >>>
    >>> Okay, that was pretty easy. Thanks for the challenge :)

    >>
    >> What's the run time speed like?

    >
    > O(1)
    >
    > r = re.compile(
    > "800-555-1212|"
    > "555-1212|"
    > r"\(800\) 555-1212"
    > )


    Mine is simpler and faster.

    r = re.compile("")
    Ian Kelly, Sep 29, 2012
    #6
  7. Re: write a regex matches 800-555-1212, 555-1212,and also (800) 555-1212.

    On Fri, 28 Sep 2012 21:25:35 -0600, Ian Kelly wrote:

    > Mine is simpler and faster.
    >
    > r = re.compile("")


    The OP doesn't say that you have to compile it, so just:

    ''

    wins.



    --
    Steven
    Steven D'Aprano, Sep 29, 2012
    #7
  8. iMath

    Tim Chase Guest

    Re: write a regex matches 800-555-1212, 555-1212, and also (800)555-1212.

    On 09/28/12 22:25, Ian Kelly wrote:
    > On Fri, Sep 28, 2012 at 8:17 PM, Tim Chase
    >>>> On 09/28/12 19:31, iMath wrote:
    >>>>> write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.

    >>
    >> r = re.compile(
    >> "800-555-1212|"
    >> "555-1212|"
    >> r"\(800\) 555-1212"
    >> )

    >
    > Mine is simpler and faster.
    >
    > r = re.compile("")


    doh! «smacks forehead» Yours is FAR more efficient, and much more
    readable than mine. iMath's teacher will be pleased :)

    -tkc
    Tim Chase, Sep 29, 2012
    #8
  9. iMath

    Tim Chase Guest

    Re: write a regex matches 800-555-1212, 555-1212, and also (800)555-1212.

    On 09/28/12 22:30, Steven D'Aprano wrote:
    > On Fri, 28 Sep 2012 21:25:35 -0600, Ian Kelly wrote:
    >
    >> Mine is simpler and faster.
    >>
    >> r = re.compile("")

    >
    > The OP doesn't say that you have to compile it, so just:
    >
    > ''
    >
    > wins.


    OP doesn't say it even has to be a string, so I guess



    wins. :p

    It's-too-late-on-a-Friday-night'ly yers,

    -tkc
    Tim Chase, Sep 29, 2012
    #9
  10. iMath

    Fg Nu Guest

    Re: write a regex matches 800-555-1212, 555-1212,and also (800) 555-1212.

    ----- Original Message -----
    From: Tim Chase <python.list@tim..thechases.com>
    To: Ian Kelly <>
    Cc: Python <>
    Sent: Saturday, September 29, 2012 9:12 AM
    Subject: Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.

    On 09/28/12 22:25, Ian Kelly wrote:
    > On Fri, Sep 28, 2012 at 8:17 PM, Tim Chase
    >>>> On 09/28/12 19:31, iMath wrote:
    >>>>> write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.

    >>
    >> r = re..compile(
    >>    "800-555-1212|"
    >>    "555-1212|"
    >>   r"\(800\) 555-1212"
    >>    )

    >
    > Mine is simpler and faster.
    >
    > r = re.compile("")


    doh!  «smacks forehead»  Yours is FAR more efficient, and much more
    readable than mine.  iMath's teacher will be pleased :)

    -tkc

    lulz.

    --
    http://mail.python.org/mailman/listinfo/python-list
    Fg Nu, Sep 29, 2012
    #10
  11. Re: write a regex matches 800-555-1212, 555-1212,and also (800) 555-1212.

    On Fri, Sep 28, 2012 at 9:58 PM, Mark Lawrence <> wrote:
    > What's the run time speed like? How much memory does it use? Shouldn't you
    > be using the regex module from pypi instead of the standard library re?
    > Guess who's borrowed the time machine?


    O(n), O(1), and I used RE2.

    -- Devin
    Devin Jeanpierre, Sep 29, 2012
    #11
  12. Re: write a regex matches 800-555-1212, 555-1212, and also (800)555-1212.

    On 29/09/2012 04:30, Steven D'Aprano wrote:
    > On Fri, 28 Sep 2012 21:25:35 -0600, Ian Kelly wrote:
    >
    >> Mine is simpler and faster.
    >>
    >> r = re.compile("")

    >
    > The OP doesn't say that you have to compile it, so just:
    >
    > ''
    >
    > wins.
    >
    >
    >


    My understanding is that Python 3.3 has regressed the performance of ''.
    Surely the Python devs can speed the performance back up and, just for
    us, use less memory at the same time?

    --
    Cheers.

    Mark Lawrence.
    Mark Lawrence, Sep 29, 2012
    #12
  13. Re: write a regex matches 800-555-1212, 555-1212,and also (800) 555-1212.

    On Sat, Sep 29, 2012 at 7:38 PM, Mark Lawrence <> wrote:
    >
    > My understanding is that Python 3.3 has regressed the performance of ''.
    > Surely the Python devs can speed the performance back up and, just for us,
    > use less memory at the same time?


    Yes, but to do that we'd have to make Python more Australia-focused
    instead of US-centric. As of Python 3.4, the empty string will be
    lazily evaluated and be delimited by redback spiders instead of
    quotes. That will give a 25% speed and 50% memory usage improvement,
    but you'll need to be careful you don't get bitten.

    ChrisA
    Chris Angelico, Sep 29, 2012
    #13
  14. Re: write a regex matches 800-555-1212, 555-1212, and also (800)555-1212.

    On 29/09/2012 11:05, Chris Angelico wrote:
    > On Sat, Sep 29, 2012 at 7:38 PM, Mark Lawrence <> wrote:
    >>
    >> My understanding is that Python 3.3 has regressed the performance of ''.
    >> Surely the Python devs can speed the performance back up and, just for us,
    >> use less memory at the same time?

    >
    > Yes, but to do that we'd have to make Python more Australia-focused
    > instead of US-centric. As of Python 3.4, the empty string will be
    > lazily evaluated and be delimited by redback spiders instead of
    > quotes. That will give a 25% speed and 50% memory usage improvement,
    > but you'll need to be careful you don't get bitten.
    >
    > ChrisA
    >


    I'll happily admit that I don't like the way this is going. Do you
    (plural) think we should take this across to python ideas?

    --
    Cheers.

    Mark Lawrence.
    Mark Lawrence, Sep 29, 2012
    #14
  15. iMath

    Ian Kelly Guest

    Re: write a regex matches 800-555-1212, 555-1212,and also (800) 555-1212.

    On Sat, Sep 29, 2012 at 3:38 AM, Mark Lawrence <> wrote:
    > My understanding is that Python 3.3 has regressed the performance of ''.
    > Surely the Python devs can speed the performance back up and, just for us,
    > use less memory at the same time?


    At least it will be stored as a Latin-1 '' for efficiency and not a
    bloated UCS-4 ''.
    Ian Kelly, Sep 29, 2012
    #15
  16. Re: write a regex matches 800-555-1212, 555-1212,and also (800) 555-1212.

    On Sun, Sep 30, 2012 at 6:51 AM, Tim Delaney
    <> wrote:
    > Personally I voted for the Fierce Snake[1][2] as the delimiter, but it was
    > voted down as "not Pythonic" enough.
    > I'm sure they were using that as a euphamism for "Python*ish*" though.
    >
    > [1] https://en.wikipedia.org/wiki/Inland_Taipan
    > [2] It's is so pretty:
    > https://upload.wikimedia.org/wikipedia/commons/f/fe/Fierce_Snake-Oxyuranus_microlepidotus.jpg


    A tempting idea, but it's rather a large delimiter. We should reserve
    that for multi-line strings, I think. Although you may have a problem
    with i18n; when you take your code to the southern hemisphere, the
    snake will be facing the other way, so what you thought was an
    open-quote marker is now a close-quote marker instead. Could get
    awkward for naive coders.

    ChrisA
    Chris Angelico, Sep 30, 2012
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. okaminer
    Replies:
    2
    Views:
    530
    IPGrunt
    Feb 16, 2005
  2. Replies:
    4
    Views:
    1,471
  3. Peter Swanson

    Re: Website width for 800 x 600 pixel

    Peter Swanson, Aug 8, 2003, in forum: HTML
    Replies:
    0
    Views:
    476
    Peter Swanson
    Aug 8, 2003
  4. Peter Swanson

    Re: Website width for 800 x 600 pixel

    Peter Swanson, Aug 8, 2003, in forum: HTML
    Replies:
    14
    Views:
    9,519
    Some One
    Aug 10, 2003
  5. Greg Ewing

    ANN: 555-BOOM! version 0.6

    Greg Ewing, Sep 25, 2007, in forum: Python
    Replies:
    0
    Views:
    263
    Greg Ewing
    Sep 25, 2007
Loading...

Share This Page