Regex Match Phrase with Different Symbols

Viewed 42

I have here a phrase AAC Barcode Description that regex should match.

The problem is sometimes I have underscore, symbols etc... like

  • AAC Barcode + Descriptions
  • AAC Barcode + Description
  • AAC Barcode & Descriptions
  • AAC Barcode and Description

REGEX

\b\w*AAC Barcode Description\w*\b
1 Answers

You can use

\w*AAC Barcode(?:\s+and\s+|\W+)Description\w*

See the regex demo. Note that word boundaries are redundant in this case when used with \w*s. If you want to match this string is a whole word, you need to remove \w*s and keep \bs:

\bAAC Barcode(?:\s+and\s+|\W+)Description\b

Details:

  • \w* - zero or more word chars
  • AAC Barcode - a fixed string
  • (?:\s+and\s+|\W+) - a non-capturing group matching either and enclosed with one or more whitespaces or one or more non-word chars
  • Description - a fixed string
  • \w* - zero or more word chars
Related