Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to write this regular expression?

Reply
Thread Tools

How to write this regular expression?

 
 
could ildg
Guest
Posts: n/a
 
      05-04-2005
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.
 
Reply With Quote
 
 
 
 
George Sakkis
Guest
Posts: n/a
 
      05-04-2005
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

 
Reply With Quote
 
 
 
 
could ildg
Guest
Posts: n/a
 
      05-04-2005
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 <(E-Mail Removed)> 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
>

 
Reply With Quote
 
Heiko Wundram
Guest
Posts: n/a
 
      05-04-2005
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-class 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.
 
Reply With Quote
 
Antoon Pardon
Guest
Posts: n/a
 
      05-04-2005
Op 2005-05-04, could ildg schreef <(E-Mail Removed)>:
> 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
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      05-04-2005
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
 
Reply With Quote
 
could ildg
Guest
Posts: n/a
 
      05-04-2005
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 <(E-Mail Removed)> wrote:
> Op 2005-05-04, could ildg schreef <(E-Mail Removed)>:
> > 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 document 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
>

 
Reply With Quote
 
could ildg
Guest
Posts: n/a
 
      05-04-2005
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 <(E-Mail Removed)> 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
>

 
Reply With Quote
 
vkeyboard
Guest
Posts: n/a
 
      05-04-2005
Personally I'd use groups.

 
Reply With Quote
 
James Stroud
Guest
Posts: n/a
 
      05-04-2005
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 <(E-Mail Removed)> 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 assessments 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
learning to write Regular Expresions =?Utf-8?B?S2VubnkgTS4=?= ASP .Net 1 06-01-2005 04:27 PM
using regular phone as voip device with regular modem? hygum VOIP 5 03-23-2005 03:51 PM
How to write Regular Expression vaidas gudas ASP .Net 1 10-04-2004 11:57 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments