-1

Is there exist regex pattern that includes (part1|part2|...) and [part] that do:

(part1|part2) will match either part 1, or part 2, e.g. leav(e|ing) matches leave and leaving

[part] is an optional word, e.g. cat[s] will match cat and cats

I also want to soild words that must be in every pattern e.g. give cat[s] will match give cat and give cats

JDoe
  • 1

1 Answers1

0

\bcats?\b will match both cat, cats but will not match cat in cater

\bleav(?:e|ing)\b will match both leave and leaving

\bpart(?:1|2|3)?\b will match part1,part2,part3 or part but not part in apart or partner

Explanation

\b     // Forces a word boundary so that it does not match in the middle of a word like part in apart
(?:    //Non capturing group so that we do not have extra groups in the matches, using this is a matter of choice
|      //OR
?      //Previous char in cats previous group in (?:1|2|3) is optional

Note that you need to escape the \ in \b while initializing the regex string.

kaza
  • 2,212
  • 1
  • 12
  • 22