$line = "start this is the start this is another end first end start hello start inside of hello end there end"; while ($line=~ s/\s*\bstart\b(?!.*?\bstart\b) (.+?) \bend\b//) { print $1, "\n"; }