> Programming Languages > Python
Various Topics Home | Disclaimer | Report Adult Posts

Various Topics on Python



Python - "How to write this regular expression?" in Programming Languages


Old 05-04-2005   #1
..u.. ....
 
Default How to write this regular expression?

I need a regular expression to check if a string matches it.
The string consists of one to there parts, each parts is a underline
followed by a number,
and the number of the first part should be 0~31, and numbers of other
parts should be
larger than 31.

The requested re should match the following strings:
_3
_5_33
_21
_29_50
_29_700_700000

And the re shouldn't match the following strings:
_3_5 the number of part 2 is less than 31
_43 the number of part 1 shouldn't be less than 31
_5_43_69_98 there shouldn't be more than 3 parts

How to write the re, please?
Thanks in advance.
 
Old 05-04-2005   #2
..or.. ..kk..
 
Default Re: How to write this regular expression?

This newsgroup is in general very helpful, but there are some
exceptions; one of them is when the problem appears blatantly to be a
homework. Perhaps if you showed that you worked on it and made some
progress, but it's not quite right, someone may help you.

George

 
Old 05-04-2005   #3
..u.. ....
 
Default Re: How to write this regular expression?

Does it matter whether it is a homework?
Why do you look down upon homework?
Everyone can do his homework well without any problems in your logic?
It's a problem I met. I tried a lot and I can't work it out,
so I came here for help.
I saw someone complained that a question is too lengthy,
and I saw some questions were complained to be unclear,
but I never saw someone waste his time to judge if a question is a
homework. If this is natural, I'll pay attention from now on.

On 4 May 2005 01:31:48 -0700, George Sakkis <gsakkis@rutgers.edu> wrote:
> This newsgroup is in general very helpful, but there are some
> exceptions; one of them is when the problem appears blatantly to be a
> homework. Perhaps if you showed that you worked on it and made some
> progress, but it's not quite right, someone may help you.
>
> George
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Old 05-04-2005   #4
..i.. ..ndr..
 
Default Re: How to write this regular expression?

On Wednesday 04 May 2005 11:34, could ildg wrote:
> Does it matter whether it is a homework?


Yes, it does matter. We're not your CS-cl*** homework monkeys... :-) We're a
forum of Python programmers who aid each other at thinking about solutions,
we don't present solutions (normally), for a beautiful example of this, see
the thread about finding similarities between two wave files...

But, anyway, as an additional hint: the stuff you need to do _can_ be solved
by an RE (the language you're matching is actually regular if you impose
several restrictions), but I'd rather not do it that way. Programming a small
function which splits the string and then does the appropriate checks (by
using int) should be much easier and faster.

And in case you really need an RE, watch this monster (to match a single term
having numerical value >= 40)...

0*(([1-9][0-9]{2,})|([4-9][0-9]))

Matching numbers >= 31 isn't hard too, I leave this as an exercise to the
reader... :-) But beware, I'd guess this regex performs rather poorly with
respect to backtracking on erraneous input such as
"00000000000000000000000030"...

--- Heiko.
 
Old 05-04-2005   #5
..to.. ..rd..
 
Default Re: How to write this regular expression?

Op 2005-05-04, could ildg schreef <could.net@gmail.com>:
> Does it matter whether it is a homework?


Yes, because if other do your homework for you, you wont
have learned anything from it.

> Why do you look down upon homework?


Who says he does. That he is not willing to do your homework
for you, doesn't imply he looks down on it.

> Everyone can do his homework well without any problems in your logic?


There is difference in asking for help on how to solve a
problem yourself and asking for the solution.

--
Antoon Pardon
 
Old 05-04-2005   #6
..t.. ..ns..
 
Default Re: How to write this regular expression?

could ildg wrote:
> I need a regular expression to check if a string matches it.


Why do you think you need a regular expression?

If another approach that involved no regular expressions worked much
better, would you reject it for some reason?

-Peter
 
Old 05-04-2005   #7
..u.. ....
 
Default Re: How to write this regular expression?

I can tell you that this is not any homework at all,
I think it by myself.

I like this maillist, it helped me a lot. but some guys as you look
weird.
On 4 May 2005 10:25:20 GMT, Antoon Pardon <apardon@forel.vub.ac.be> wrote:
> Op 2005-05-04, could ildg schreef <could.net@gmail.com>:
> > Does it matter whether it is a homework?

>
> Yes, because if other do your homework for you, you wont
> have learned anything from it.
>
> > Why do you look down upon homework?

>
> Who says he does. That he is not willing to do your homework
> for you, doesn't imply he looks down on it.
>
> > Everyone can do his homework well without any problems in your logic?

>
> There is difference in asking for help on how to solve a
> problem yourself and asking for the solution.

I read the do***ent about re on python tommorow, and when I want to
use it to settle a problem, I found it not so easy, so I raise the question
here. I didn't say how I thought about it, because I don't want the question
to be too long. But a short question doesn't mean that I am too lazy
and I didn't even think about it. If you think I'm a kind of person
you hate to help,
you needn't.
>
> --
> Antoon Pardon
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Old 05-04-2005   #8
..u.. ....
 
Default Re: How to write this regular expression?

Thank you.

I just learned how to use re, so I want to find a way to settle it by
using re. I know that split it into pieces will do it quickly.

On 5/4/05, Peter Hansen <peter@engcorp.com> wrote:
> could ildg wrote:
> > I need a regular expression to check if a string matches it.

>
> Why do you think you need a regular expression?
>
> If another approach that involved no regular expressions worked much
> better, would you reject it for some reason?
>
> -Peter
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Old 05-04-2005   #9
..eyboa..
 
Default Re: How to write this regular expression?

Personally I'd use groups.

 
Old 05-04-2005   #10
..m.. ..ro..
 
Default Re: How to write this regular expression?

On Wednesday 04 May 2005 02:34 am, so sayeth could ildg:
> I saw someone complained that a question is too lengthy,
> and I saw some questions were complained to be unclear,
> but I never saw someone waste his time to judge if a question is a
> homework. If this is natural, I'll pay attention from now on.
>
> On 4 May 2005 01:31:48 -0700, George Sakkis <gsakkis@rutgers.edu> wrote:
> > This newsgroup is in general very helpful, but there are some
> > exceptions; one of them is when the problem appears blatantly to be a
> > homework. Perhaps if you showed that you worked on it and made some
> > progress, but it's not quite right, someone may help you.


I think by participating in this list, most of the members have felt that they
have agreed to the following unofficial terms and conditions of use:

http://www.catb.org/~esr/faqs/smart-questions.html

The interesting thing is that those who follow the letter most strictly are
usually the best ones to ask. Moreover, most members of this list are usually
looking for any excuse to compose a regular expression. In fact, they
probably come up with an answer before they make any ***essments about
homework.

> I can tell you that this is not any homework at all,
> I think it by myself.


In that case, your question is free game:


>>> r = re.compile(r"_[0-3]\d?(_\d\d?){0,2}")
>>>
>>> r.search('_29_700_700000')

<_sre.SRE_Match object at 0x402ccba0>
>>> r.search('_29_50')

<_sre.SRE_Match object at 0x402f8820>
>>> r.search('_5_33')

<_sre.SRE_Match object at 0x402ccba0>
>>> r.search('_500')
>>>


James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 

Thread Tools
Display Modes





Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0