use Data::Dumper; $regex = qr< \b (?: # Non capturing group ## Case 1: currency|foreign exchange comes second (revenues?|sales|growth) # group 1 \W+ (?:\w+\W+){0,4}? # Non capturing group (currency|foreign\Wexchange) # group 2 | ## Case 2: currency|foreign exchange comes first (currency|foreign\Wexchange) # group 3 \W+ (?:\w+\W+){0,4}? # Non capturing group (revenues?|sales|growth) # group 4 ) \b >x; $text = <