while ($text =~ /(\b$pattern1\b)|(\b$pattern2\b)/g) { # Do something with $+ }