-2

How can I make regular expression to match if any of the substrings is at any point of the string?

I mean if I define substrings "one" and "two" these are a match:

  • One
  • Two
  • Blah one blah
  • foo bar two baz.

So it also needs to be case insensitive. I need this in auto reply app on Android so I can only use one regular expression.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Try `(?i)\b(?:one|two)\b`. The word boundaries (`\b`) are to prevent matching `"one"` in `"toner"` or `"two"` in `"trustworthy"`, for example. – Cary Swoveland Jun 30 '20 at 18:38
  • This only works if it's exact match (one) but doesn't work in other cases (blah one blah). – Joni Hietala Jun 30 '20 at 19:31
  • Did I misunderstand the question? Don't you just want to know if the string contains the word `"one"` or `"two"` (case-indifferent)? [ref](https://regex101.com/r/arEf79/1/). Move your cursor through the regex at the link for details. – Cary Swoveland Jun 30 '20 at 19:52
  • You understood correctly. Looks like it works in here but not in my app. Only strings "one" and "two" trigger my responder app to reply. Probably a bug or is there some version issues on regex or other kind of incompatibility? – Joni Hietala Jun 30 '20 at 21:05
  • I can't really say without more information. Are you saying the regex I suggested responds to `"one"` but not `"One"` (i.e., case-indifference is the problem)? Keep in mind that I tested with the PCRE (PHP) regex engine. `(?i)` in the regex might not be recognized by your Android application (but not raise an error). If so there may another way to set the case-indifference flag. Can you enter `/\b(?:one|two)\b/i`? – Cary Swoveland Jun 30 '20 at 22:57
  • Casing doesn't matter. It only works with one word: one/two. If there is any characters/words around the word it doesn't work. – Joni Hietala Jul 01 '20 at 06:09

1 Answers1

-1

Here is the expression:

/(one|two)\b/i

The i flag denotes case-insensitivity