[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: A little regexp help for a newbie.

Daniel Baird

8/4/2006 5:32:00 AM

On 8/4/06, RJ Melton <shavah@gmail.com> wrote:
> Anyway, I have a simple string in the format 1a2+3 and want to basically
> take out the non-number bits and put the numbers into an array.
> It also has to match more than single-digit numbers.

I'm pretty sure this is non-optimal, but you could use String.split:

"1a23+4-123".split(/[^\d]/)

the argument to split is a regex that matches anything that's not a
digit ( \d is a digit; [^\d] is anything but that). split uses that
argument to decide where to split the string up.

;D

--
Daniel Baird
http://tidd... (free, effortless TiddlyWiki hosting)
http://danie... (TiddlyW;nks! :: Whiteboard Koala :: Blog ::
Things That Suck)

4 Answers

Robert Retzbach

8/4/2006 5:44:00 AM

0

What about "1a2+b".scan(/\d+) ?
It returns the numbers as array.

RJ Melton schrieb:
> I tried that and it works fine, optimal or not. Thanks. :)
>
> On 8/3/06, Daniel Baird <danielbaird@gmail.com> wrote:
>>
>> On 8/4/06, RJ Melton <shavah@gmail.com> wrote:
>> > Anyway, I have a simple string in the format 1a2+3 and want to
>> basically
>> > take out the non-number bits and put the numbers into an array.
>> > It also has to match more than single-digit numbers.
>>
>> I'm pretty sure this is non-optimal, but you could use String.split:
>>
>> "1a23+4-123".split(/[^\d]/)
>>
>> the argument to split is a regex that matches anything that's not a
>> digit ( \d is a digit; [^\d] is anything but that). split uses that
>> argument to decide where to split the string up.
>>
>> ;D
>>
>> --
>> Daniel Baird
>> http://tidd... (free, effortless TiddlyWiki hosting)
>> http://danie... (TiddlyW;nks! :: Whiteboard Koala :: Blog ::
>> Things That Suck)
>>
>>
>
>

Daniel Baird

8/4/2006 5:51:00 AM

0

On 8/4/06, Robert Retzbach <rretzbach@googlemail.com> wrote:
> What about "1a2+b".scan(/\d+) ?
> It returns the numbers as array.

Muuuuch better!

--
Daniel Baird
http://tidd... (free, effortless TiddlyWiki hosting)
http://danie... (TiddlyW;nks! :: Whiteboard Koala :: Blog ::
Things That Suck)

Daniel Baird

8/4/2006 6:01:00 AM

0

> > On 8/4/06, Robert Retzbach <rretzbach@googlemail.com> wrote:
> > > What about "1a2+b".scan(/\d+) ?
> > > It returns the numbers as array.
>
> Will this pick up negative numbers or floats?
>

Nope, only plain digits. if you want other stuff, you need to add it
to the regex.
/\d+/ ..is just digits
/[+-]?\d+/ ..optional + or -
/[+-]?\d+\.?\d*/ ..optional + or -, and optional .

untested.. (sorry, too lazy to test :)

--
Daniel Baird
http://tidd... (free, effortless TiddlyWiki hosting)
http://danie... (TiddlyW;nks! :: Whiteboard Koala :: Blog ::
Things That Suck)

Robert Retzbach

8/4/2006 2:13:00 PM

0

p "-1.2 56 0.443 4.22E-12 000555".scan(
/(?:[+-])?[0-9]+(?:[,.][0-9]+)?(?:E[+-][1-9][0-9]*)?/ )
#=> ["-1.2", "56", "0.443", "4.22E-12", "000555"]

This looks okay for most purposes, in my opinion. :)
You can google for "real number regex" et cetera...

Daniel Baird schrieb:
>> > On 8/4/06, Robert Retzbach <rretzbach@googlemail.com> wrote:
>> > > What about "1a2+b".scan(/\d+) ?
>> > > It returns the numbers as array.
>>
>> Will this pick up negative numbers or floats?
>>
>
> Nope, only plain digits. if you want other stuff, you need to add it
> to the regex.
> /\d+/ ..is just digits
> /[+-]?\d+/ ..optional + or -
> /[+-]?\d+\.?\d*/ ..optional + or -, and optional .
>
> untested.. (sorry, too lazy to test :)
>