$var="Files Checked IN: test/abc.txt 1.23 1.3.4.5 team/hello.cpp 2.1 NONE Clear/thing.pl NONE 1.2.34 etc etc"; my $pattern = '([\w./]+)\s+([\d.]+|NONE)\s+([\d.]+|NONE)'; if ( ( @matches = $var =~ m{$pattern}g ) >0 ) { print join('|',@matches),$/; }