in reply to Matching a pattern two or four times
Also your regex for floating point numbers leaves quite a bit to be desired. For instance it will match ip addresses as well as floating point values (not to mention things like "................."). There are a variety of regexes that will handle numbers like this correctly to be found in the FAQS.
Oh, I changed it to be tolerant of leading and trailing whitespace. YMMV.my $num_rex=qr/(-?(?:\d+(?:\.\d*)?|\.\d+))/; # modified from: perldoc +-q scalar is a number while (<DATA>) { if (/^\s* (\w+) \s+ $num_rex \s+ $num_rex (?: \s+ $num_rex \s+ $num_ +rex )? \s*$/x) { print "Matched a word and ",(defined $4?"four":"two")," numbers: +$1 $2 $3",(defined $4?" $4 $5\n":"\n"); } } __DATA__ Abc 21223.7 21225.33 22270.3 22280.1 Def 21600.23 24567.43
HTH
--- demerphq
my friends call me, usually because I'm late....
|
|---|