use Regexp::Common;
/$RE{number}{int}/;
Abigail | [reply] [d/l] |
I dont think you need a module to make that, any way 01 or 1 is still a number and zero is a neutral number.
#!/usr/bin/perl -w
$_="Testing numbers 12";
print "Numbers: <$1>" if ( /(\d*)/ );
I think this might work
| [reply] [d/l] |
But it doesn't work, that's exactly the reason why you should use a module!
Zaxo has a 'working' hand-rolled solution somewhere else in this thread, though it breaks on negative numbers (which might or might not be OK, depending on the input data). Granted, in this simple case it might be OK not to use a module but still you have to know what you are doing. In all but the most trivial cases, use the module and spare yourself the extra debugging work. Try e.g. to match a general floating point number like -2.4345e-12 and then you know why it's better/easier/quicker to use a module.
-- Hofmator
| [reply] |
my ($num) = $string =~ /(\d+)/;
# Added
$num += 0;
$num is in parens to put the right side into list context, which makes match return the value matched.
After Compline, Zaxo | [reply] [d/l] |
That would make "05 stringstring" return 05, though, not 5.
Maybe something like:
my ($num) = $string =~ /0*(\d+)/;
That would also correctly set $num to 0 for a string like "string0string", right?
Queue
still making his way through Mastering Regular Expressions
| [reply] [d/l] [select] |
| [reply] [d/l] |