Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Defining re pattern for matching list of numbers

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


jjkk73 at gmail

Nov 6, 2009, 8:04 AM

Post #1 of 4 (222 views)
Permalink
Defining re pattern for matching list of numbers

Hi,
I'm trying to write a re pattern to match a list of one or more numbers,
each number is in the range of 1-15 value and the numbers are separated by
spaces (but there are no spaces at the beginning and end of the string). For
instance:

"3"
"1 14 8"

but not:

"22"
" 5 11"

I've come up with something like this

re.compile("^((([1-9])|(1[0-8]))( +(?=[1-9]))*)+$")

but I can't believe this has to be so complicated.
Does anyone know some simpler re pattern to match this kind of string
Many thanks


clp2 at rebertia

Nov 6, 2009, 10:16 AM

Post #2 of 4 (199 views)
Permalink
Re: Defining re pattern for matching list of numbers [In reply to]

On Fri, Nov 6, 2009 at 8:04 AM, jorma kala <jjkk73 [at] gmail> wrote:
> Hi,
> I'm trying to write a re pattern to match a list of one or more numbers,
> each number is in the range of 1-15 value and the numbers are separated by
> spaces (but there are no spaces at the beginning and end of the string). For
> instance:
>
> "3"
> "1 14 8"
>
> but not:
>
> "22"
> " 5 11"
>
> I've come up with something like this
>
> re.compile("^((([1-9])|(1[0-8]))( +(?=[1-9]))*)+$")
>
> but I can't believe this has to be so complicated.

Your format seems so simple I have to ask why you're using regexes in
the first place.

try:
nums = [int(num) for num in line.split(" ")]
except ValueError:
print "Invalid input"

for num in nums:
if num < 1 or num > 15:
raise ValueError, "Number present which is outside of valid range"

Cheers,
Chris
--
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


jjkk73 at gmail

Nov 6, 2009, 11:12 AM

Post #3 of 4 (197 views)
Permalink
Re: Defining re pattern for matching list of numbers [In reply to]

Thanks for your reply.
But I need to use a regex; my program deals with a large number of
patterns, and I want to treat them in a uniform way (through regex).

On 11/6/09, Chris Rebert <clp2 [at] rebertia> wrote:
>
> On Fri, Nov 6, 2009 at 8:04 AM, jorma kala <jjkk73 [at] gmail> wrote:
> > Hi,
> > I'm trying to write a re pattern to match a list of one or more numbers,
> > each number is in the range of 1-15 value and the numbers are separated
> by
> > spaces (but there are no spaces at the beginning and end of the string).
> For
> > instance:
> >
> > "3"
> > "1 14 8"
> >
> > but not:
> >
> > "22"
> > " 5 11"
> >
> > I've come up with something like this
> >
> > re.compile("^((([1-9])|(1[0-8]))( +(?=[1-9]))*)+$")
> >
> > but I can't believe this has to be so complicated.
>
>
> Your format seems so simple I have to ask why you're using regexes in
> the first place.
>
> try:
> nums = [int(num) for num in line.split(" ")]
> except ValueError:
> print "Invalid input"
>
> for num in nums:
> if num < 1 or num > 15:
> raise ValueError, "Number present which is outside of valid range"
>
> Cheers,
> Chris
>
> --
> http://blog.rebertia.com
>


steve at REMOVE-THIS-cybersource

Nov 7, 2009, 7:14 AM

Post #4 of 4 (188 views)
Permalink
Re: Defining re pattern for matching list of numbers [In reply to]

On Fri, 06 Nov 2009 10:16:31 -0800, Chris Rebert wrote:

> Your format seems so simple I have to ask why you're using regexes in
> the first place.

Raymond Hettinger has described some computing techniques as "code
prions" -- programming advice or techniques which are sometimes useful
but often actively harmful.

http://www.mail-archive.com/python-list%40python.org/msg262651.html

As useful as regexes are, I think they qualify as code prions too: people
insist on using them in production code, even when a simple string method
or function would do the job far more efficiently and readably.



--
Steven
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.