#!/usr/bin/perl -w use strict; while( ) { if( m{^\d+ # initial digits (1rst field) \s+ # separating space(s) (.*?) # the 2cd and 3rd field \s+ # separating space(s) \S # 1 (non-space) character (4th field) \s+ # separating space(s) \S # 1 (non-space) character (5th field) \s* # you might or might not want to allow extra spaces a the end of the line $}x ) { my $fields= $1; my @fields; # @fields= heuristic1( $fields) || heuristic2( $fields); # does not work as the || seems to put the first function call in scalar mode, weird @fields= heuristic1( $fields); unless( @fields) { @fields= heuristic2( $fields); } if( @fields) { print "field 2: '$fields[0]' - field 3: '$fields[1]'\n"; } else { warn "cannot extract field 2/3 of line $. Fields are: '$fields'\n"; } } else { warn "can't parse line $."; } } # only 2 words, that's easy sub heuristic1 { my $fields= shift; my @fields= split /\s+/, $fields; if( @fields == 2) { return @fields } else { return; } } # more than one space separates the 2 fields sub heuristic2 { my $fields= shift; my @fields= split /\s\s+/, $fields; if( @fields == 2) { return @fields } else { return; } } __DATA__ 122 Genesis Chamber Mark Tedin A U 123 f2w f3w 4 5 123 f2w1 f2w2 f3w 4 5 123 f2w1 f2w2 f3w1 f3w2 4 5 123 f2w f3w 4 99 123 f2w1 f2w2 f3w1 f3w2 4 5