-1

I want to search any of the three different strings and their variations on a given string.

If any of these three strings CAT-17*, or CAT-19*, or CAT-20* are there on a given string, then there is a match. Here the asterisk means zero or more characters, were the string may have subsequent characters (example: CAT-17, or CAT-17J1, or CAT-19, or CAT-19F2, or CAT-20, or CAT-20MM3)

I have tried the below regex and it is matching CAT-20J1 but not CAT-20, same goes for other strings also. if I add some subsequent characters it is matching, but not when the string is alone.

\b(?:CAT-17([\w]+)|CAT-19([\w]+)|CAT-20([\w]+))\b

How can I fix this for regex to accept 0 or more characters after the main string, for example, CAT-20

Dan
  • 425
  • 1
  • 5
  • 13

1 Answers1

1

My first suggestion: Regex101 demo

/\bCAT-(17|19|20)\w*\b/g

Explained:

  • \b Word boundary
  • CAT- matches exactly "CAT-"
  • (17|19|20) followed by one of this group options
  • \w word character (equal to [a-zA-Z0-9_])
  • * zero or more times (Here's the catch!)
  • \b till a word boundary.
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313