#! perl -slw use strict; my @lines = ( '111 MatchMe [This is why]', '222 I need to be dispatched to the same callback: For This Reason', ); for ( @lines ) { m/ ^(\d+) \s (?: (?-x:I need to be dispatched.*?: ) | (?-x:MatchMe \[) ) (.*?) \]? $ /x and print "'$1'$2'"; } __END__ C:\test>junk '111'This is why' '222'For This Reason'