[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: pattern matching question

Tony Targonski

4/13/2005 5:16:00 PM

-----Original Message-----
From: Automat.Svet@gmail.com [mailto:Automat.Svet@gmail.com]
Sent: Wednesday, April 13, 2005 1:05 PM
To: ruby-talk ML
Subject: pattern matching question

I' mtrying to match a pattern like this one : 297,12^[2 where 297,12 is
the grand total.

Thanks in advance
automat_svet
----------------------------

your_text.scan(/([0-9]+,[0-9]+)\^\[2/)

Will return [["297,12"]], so you might want to stick a .flatten[0] in
there if you know you'll have only a single match.

--Tony







1 Answer

Nikolai Weibull

4/13/2005 6:03:00 PM

0

Tony Targonski, April 14:

> > I' mtrying to match a pattern like this one : 297,12^[2 where 297,12
> > is the grand total.

Are you sure all numbers will be formatted in the \d+,\d+ format? What
about . in the first set? What about numbers lacking the decimal part?

> your_text.scan(/([0-9]+,[0-9]+)\^\[2/)

> Will return [["297,12"]], so you might want to stick a .flatten[0] in
> there if you know you'll have only a single match.

Use (?:â?¦) instead (and \d as it's a lot faster and clearer):

your_text.scan(/(?:\d+,\d+)\^\[2/)

Enjoy,
nikolai

--
Nikolai Weibull: now available free of charge at http:/...!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}