0

I have a String

man loves mango. heman is a hero.

now I want to get the word man

but it's selecting man, mango, heman

how do I make sure that the word man is only selected?

the regex I'm using [^(.*?)]\s(man) (It's wrong)

1 Answers1

0

Use a word boundary

\bman\b

This matches exactly the word man.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • I need some help here. It's working on https://regex101.com/ but for some reason not working with java (android). –  Apr 08 '20 at 18:32
  • I don't know what java does differently in this case. If you have a language specific regex question, you should use the appropriate tag. – cigien Apr 08 '20 at 18:49
  • 1
    i found the solution, we just need to use double slash instead of single ```\\bman\\b``` –  Apr 08 '20 at 18:58
  • Note that the extra backslashes are not a part of the regex. Backslash is probably an escape sequence, so the extra backslash is needed to escape the backslash itself. – cigien Apr 08 '20 at 19:00