Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #30439 > unrolled thread

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

Started byiMath <redstone-cold@163.com>
First post2012-09-28 17:31 -0700
Last post2012-09-28 21:25 -0700
Articles 16 — 9 participants

Back to article view | Back to comp.lang.python


Contents

  write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212. iMath <redstone-cold@163.com> - 2012-09-28 17:31 -0700
    Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212. Tim Chase <python.list@tim.thechases.com> - 2012-09-28 20:35 -0500
    Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212. Paul Rubin <no.email@nospam.invalid> - 2012-09-28 18:42 -0700
    Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212. Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-29 02:58 +0100
    Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212. Tim Chase <python.list@tim.thechases.com> - 2012-09-28 21:17 -0500
    Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212. Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-28 21:25 -0600
      Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-29 03:30 +0000
        Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212. Tim Chase <python.list@tim.thechases.com> - 2012-09-28 22:50 -0500
        Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212. Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-29 10:38 +0100
        Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212. Chris Angelico <rosuav@gmail.com> - 2012-09-29 20:05 +1000
        Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212. Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-29 11:50 +0100
        Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212. Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-29 11:04 -0600
        Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212. Chris Angelico <rosuav@gmail.com> - 2012-09-30 09:26 +1000
    Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212. Tim Chase <python.list@tim.thechases.com> - 2012-09-28 22:42 -0500
    Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212. Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-09-29 00:25 -0400
    Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212. Fg Nu <fgnu32@yahoo.com> - 2012-09-28 21:25 -0700

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

FromiMath <redstone-cold@163.com>
Date2012-09-28 17:31 -0700
Subjectwrite a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.
Message-ID<de40c5ae-e51f-4b21-9355-da06a4882c89@googlegroups.com>
write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.

[toc] | [next] | [standalone]


#30450

FromTim Chase <python.list@tim.thechases.com>
Date2012-09-28 20:35 -0500
Message-ID<mailman.1596.1348882482.27098.python-list@python.org>
In reply to#30439
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


[toc] | [prev] | [next] | [standalone]


#30452

FromPaul Rubin <no.email@nospam.invalid>
Date2012-09-28 18:42 -0700
Message-ID<7xhaqh8uzv.fsf@ruckus.brouhaha.com>
In reply to#30439
iMath <redstone-cold@163.com> writes:
> write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.

And then you have two problems.

[toc] | [prev] | [next] | [standalone]


#30453

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2012-09-29 02:58 +0100
Message-ID<mailman.1597.1348883794.27098.python-list@python.org>
In reply to#30439
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.

[toc] | [prev] | [next] | [standalone]


#30456

FromTim Chase <python.list@tim.thechases.com>
Date2012-09-28 21:17 -0500
Message-ID<mailman.1599.1348884961.27098.python-list@python.org>
In reply to#30439
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

[toc] | [prev] | [next] | [standalone]


#30460

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-09-28 21:25 -0600
Message-ID<mailman.1600.1348889168.27098.python-list@python.org>
In reply to#30439
On Fri, Sep 28, 2012 at 8:17 PM, Tim Chase
<python.list@tim.thechases.com> 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("")

[toc] | [prev] | [next] | [standalone]


#30461

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-09-29 03:30 +0000
Message-ID<50666b49$0$29981$c3e8da3$5496439d@news.astraweb.com>
In reply to#30460
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


[toc] | [prev] | [next] | [standalone]


#30464

FromTim Chase <python.list@tim.thechases.com>
Date2012-09-28 22:50 -0500
Message-ID<mailman.1603.1348890545.27098.python-list@python.org>
In reply to#30461
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

[toc] | [prev] | [next] | [standalone]


#30472

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2012-09-29 10:38 +0100
Message-ID<mailman.1612.1348911398.27098.python-list@python.org>
In reply to#30461
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.

[toc] | [prev] | [next] | [standalone]


#30473

FromChris Angelico <rosuav@gmail.com>
Date2012-09-29 20:05 +1000
Message-ID<mailman.1613.1348913142.27098.python-list@python.org>
In reply to#30461
On Sat, Sep 29, 2012 at 7:38 PM, Mark Lawrence <breamoreboy@yahoo.co.uk> 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

[toc] | [prev] | [next] | [standalone]


#30474

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2012-09-29 11:50 +0100
Message-ID<mailman.1614.1348915614.27098.python-list@python.org>
In reply to#30461
On 29/09/2012 11:05, Chris Angelico wrote:
> On Sat, Sep 29, 2012 at 7:38 PM, Mark Lawrence <breamoreboy@yahoo.co.uk> 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.

[toc] | [prev] | [next] | [standalone]


#30512

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-09-29 11:04 -0600
Message-ID<mailman.1642.1348938327.27098.python-list@python.org>
In reply to#30461
On Sat, Sep 29, 2012 at 3:38 AM, Mark Lawrence <breamoreboy@yahoo.co.uk> 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 ''.

[toc] | [prev] | [next] | [standalone]


#30531

FromChris Angelico <rosuav@gmail.com>
Date2012-09-30 09:26 +1000
Message-ID<mailman.1656.1348961183.27098.python-list@python.org>
In reply to#30461
On Sun, Sep 30, 2012 at 6:51 AM, Tim Delaney
<timothy.c.delaney@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#30463

FromTim Chase <python.list@tim.thechases.com>
Date2012-09-28 22:42 -0500
Message-ID<mailman.1602.1348890078.27098.python-list@python.org>
In reply to#30439
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

[toc] | [prev] | [next] | [standalone]


#30465

FromDevin Jeanpierre <jeanpierreda@gmail.com>
Date2012-09-29 00:25 -0400
Message-ID<mailman.1605.1348892793.27098.python-list@python.org>
In reply to#30439
On Fri, Sep 28, 2012 at 9:58 PM, Mark Lawrence <breamoreboy@yahoo.co.uk> 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

[toc] | [prev] | [next] | [standalone]


#30467

FromFg Nu <fgnu32@yahoo.com>
Date2012-09-28 21:25 -0700
Message-ID<mailman.1607.1348892859.27098.python-list@python.org>
In reply to#30439



----- Original Message -----
From: Tim Chase <python.list@tim.thechases.com>
To: Ian Kelly <ian.g.kelly@gmail.com>
Cc: Python <python-list@python.org>
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web