while ($text =~ m/'(((\\')|[^'])*)'/g) { if (length($1) > 3) { ...do something... } } #### while ($text =~ m/'(((\\['\\])|[^'])*)'/g) {