How to match multiple words?

To match multiple words in a regular expression, you can use the alternation operator |, which acts like a logical OR. You can list the words you want to match, separated by the | symbol.

For example, to match the words "apple", "banana", and "cherry", you can use the following regular expression:

\b(apple|banana|cherry)\b

This pattern will match any of the three words as standalone words. The \b ensures that the match occurs at word boundaries.

Here are some examples:

  • The string "I have an apple." will match.
  • The string "I like banana and cherry." will match.
  • The string "I have a fruit salad." will not match.

If you want to match multiple words regardless of their order or position in the string, you can use a more complex pattern or combine them with other regex features as needed.

0 Comments

no data
Be the first to share your comment!