Hi!
just for fun, I played around with this a little.
#!/usr/bin/perl while (<>) { print if m/^([+-]?\d*\.\d*)\s+([+-]?\d*.\d*)$/; print "$1 ~ ", int $1, "\n$2 ~ ", int $2, "\n"; print "$_\n" for (int $1 .. int $2); }
Putting the regex between ^$ makes input like .1 2foo illegal (and would have fixed your regexp, too, BTW). [+-]? allows to have a positive, negative or no sign. So this now takes all of 1 .1 1. 1.1 . all prefixed with + or - or nothing. Note that . is fine -- int converts it to 0.
int $1 .. int $2 make it work with input like 0.3 4 (where your snipplet was broken).
The \s+ (one or more whitespace was just because I was annoyed by having to type "to" all the time during playing around. ;)
Hope you find all that interesting.. ;)
So long,
Flexx
In reply to Re: Matching two real numbers in a string using regex
by Flexx
in thread Matching two real numbers in a string using regex
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |