Sorry this is such an absurdly simple question, but I can't find the answer
after some googling, probably just me not knowing how to look.
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. The rest is simple
enough, but I'm just really new to regex and can't make it work.
Anyone feel like helping me out a little, or at least pointing me in the
right direction? Thanks.
···
--
R.J. Melton
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
···
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.
--
Daniel Baird
http://tiddlyspot.com (free, effortless TiddlyWiki hosting)
http://danielbaird.com (TiddlyW;nks! :: Whiteboard Koala :: Blog ::
Things That Suck)
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://tiddlyspot.com (free, effortless TiddlyWiki hosting)
http://danielbaird.com (TiddlyW;nks! :: Whiteboard Koala :: Blog ::
Things That Suck)
--
R.J. Melton
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://tiddlyspot.com (free, effortless TiddlyWiki hosting)
http://danielbaird.com (TiddlyW;nks! :: Whiteboard Koala :: Blog ::
Things That Suck)
Will this pick up negative numbers or floats?
···
On 8/4/06, Daniel Baird <danielbaird@gmail.com> wrote:
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://tiddlyspot.com (free, effortless TiddlyWiki hosting)
http://danielbaird.com (TiddlyW;nks! :: Whiteboard Koala :: Blog ::
Things That Suck)
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 
···
> 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?
--
Daniel Baird
http://tiddlyspot.com (free, effortless TiddlyWiki hosting)
http://danielbaird.com (TiddlyW;nks! :: Whiteboard Koala :: Blog ::
Things That Suck)
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 