in reply to Matching a pattern two or four times

Personally I wonder at what you are doing. If your file contains only records in the above format then use split as other monks have suggested. However if you are trying to extract lines that match from a bunch of other crud then the regex will have to be the way to go.

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.

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
Oh, I changed it to be tolerant of leading and trailing whitespace. YMMV.

HTH

--- demerphq
my friends call me, usually because I'm late....