[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Regular expression reformating a value

Steven Ketcham

9/28/2006 4:54:00 PM

What I am looking for is a single regex to accomplish this so I can avoid
the parsing afterwards.

I would like to break the string into groups then reformat the string with
some regex conditional logic. Is this possible?


Thanks!



Steve






-----Original Message-----
From: Robert Dober [mailto:robert.dober@gmail.com]
Sent: Thursday, September 28, 2006 12:28 PM
To: ruby-talk ML
Subject: Re: Regular expression reformating a value

On 9/28/06, Steven Ketcham <steve@encite.us> wrote:
>
>
>
> I am not really sure how to do this. I have a series of number that need
> parsed. Strip the zeros, if a caret
> exists replace it with a slash.
>
> 1.) 000123^2 - should be 123/2
> 2.) 1^2 - should be 1/2
> 3.) 0001 - should be 1
> 4.) 000123 - should be 123


How about
s.sub!(/^\s*0+/,"").sub(/\^/,"/")
than
if you need checking
%r{^\s*[0-9/]+} === s
which is not completely strict because of
5.) 12^34/5

Cheers
Robert


> I do not know how to do the condtional to exclude the slash on examples 3
> and 4. How do I prevent the addition of the slash when there is no caret
> in
> the number?
>
>
>
>
>
> reg = /(?:[1-9]{1}\d*)\W(?:\d*)/
>
> puts reg.match("000123^2");
>
> puts reg.match("123^2");
>
> puts reg.match("0001");
>
> puts reg.match("000123");
>
> puts reg.match("123");
>
>
>
> Thanks!
>
>
>
>
>
>
>
>
>
>
>


--
Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein