$pattern = 'AB'; 'BABABABBB' =~ / ^ # start at beginning of line ( # capture to $1 .*? # a number of character, but as few as possible ... (?$3$/"; __END__ prints "BBB"