#! /usr/bin/perl $str = "aaaaa\nbbbbb\nccccc\naaaaa\nddddd\neeeee\n" ; @a = $str =~ /aaaaa(?:(?!aaaaa).)*/gs ; print "$a[0]\n$a[1]" ;