Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

97 views
Skip to first unread message

iMath

unread,
Sep 28, 2012, 8:31:40 PM9/28/12
to
write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.

Tim Chase

unread,
Sep 28, 2012, 9:35:41 PM9/28/12
to iMath, pytho...@python.org
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



Paul Rubin

unread,
Sep 28, 2012, 9:42:44 PM9/28/12
to
iMath <redsto...@163.com> writes:
> write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.

And then you have two problems.

Mark Lawrence

unread,
Sep 28, 2012, 9:58:51 PM9/28/12
to pytho...@python.org
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.

Tim Chase

unread,
Sep 28, 2012, 10:17:07 PM9/28/12
to Mark Lawrence, pytho...@python.org
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


Ian Kelly

unread,
Sep 28, 2012, 11:25:35 PM9/28/12
to Python
On Fri, Sep 28, 2012 at 8:17 PM, Tim Chase
<pytho...@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("")

Steven D'Aprano

unread,
Sep 28, 2012, 11:30:17 PM9/28/12
to
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



Tim Chase

unread,
Sep 28, 2012, 11:42:23 PM9/28/12
to Ian Kelly, Python
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

unread,
Sep 28, 2012, 11:50:10 PM9/28/12
to Steven D'Aprano, pytho...@python.org
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


Devin Jeanpierre

unread,
Sep 29, 2012, 12:25:50 AM9/29/12
to Mark Lawrence, pytho...@python.org
On Fri, Sep 28, 2012 at 9:58 PM, Mark Lawrence <bream...@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

Fg Nu

unread,
Sep 29, 2012, 12:25:26 AM9/29/12
to Tim Chase, Ian Kelly, Python

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

Mark Lawrence

unread,
Sep 29, 2012, 5:38:17 AM9/29/12
to pytho...@python.org
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.

Chris Angelico

unread,
Sep 29, 2012, 6:05:33 AM9/29/12
to pytho...@python.org
On Sat, Sep 29, 2012 at 7:38 PM, Mark Lawrence <bream...@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

Mark Lawrence

unread,
Sep 29, 2012, 6:50:04 AM9/29/12
to pytho...@python.org
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.

Ian Kelly

unread,
Sep 29, 2012, 1:04:55 PM9/29/12
to Python
On Sat, Sep 29, 2012 at 3:38 AM, Mark Lawrence <bream...@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 ''.

Chris Angelico

unread,
Sep 29, 2012, 7:26:20 PM9/29/12
to pytho...@python.org
On Sun, Sep 30, 2012 at 6:51 AM, Tim Delaney
<timothy....@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
0 new messages