use strict; use warnings; while ( ) { my $string = $_; if ( $string =~ /func[A-Za-z]\( (func[A-Za-z]\([^\)]+\)|[^,]+) #looks for a function and if not found then ,(func[A-Za-z]\([^\)]+\)|[^,]+) #grab everything to the comma ,(func[A-Za-z]\([^\)]+\)|[^,]+) ,(func[A-Za-z]\([^\)]+\)|[^,]+)\)/x) { print "FOR STRING: $string", '$1 = ',$1,$/, '$2 = ',$2,$/, '$3 = ',$3,$/, '$4 = ',$4,$/,$/; } } __DATA__ funca(1,2,3,4) funca(funcH(A,B,C),2,3,4) funca(1,funcH(A,B,C),3,4) funca(1,2,funcH(A,B,C),4) funca(1,2,3,funcH(A,B,C))