' tom harry ' =~ m/(?=.*tom).*(dick|harry|john)/ && print('A', $/); # A ' harry tom ' =~ m/(?=.*tom).*(dick|harry|john)/ && print('B', $/); # B