#!/usr/local/bin/perl use strict; use warnings; $_ = <<'EOT'; // useless comment Some Code //********************************************************** // //I need that text // And that also // //********************************************************** Some Code //********************************************************** // //I need that text // And that also // //********************************************************** Some Code // useless comment Some Code //********************************************************** // //I need that text // And that also // And some more // //********************************************************** Some Code // useless comment EOT my $i = 0; while (m|^//\*+\s*^.+?(\w[^*]+\w).+?^//\*+\s*$|smg) { printf "%d:\n%s\n\n", ++$i, $1; } __END__ 1: I need that text // And that also 2: I need that text // And that also 3: I need that text // And that also // And some more #### ^//\*+\s+^.+?^//\s*(\w.+?)\s*^//\s*(\w.+?$).+?^//\*+\s+$