2

In php I can check if a word is in a String like that

$muster = "/\b($word)\b/i";
if(preg_match($muster, $text)) {
        return true;
}

Example:

$word = "test";
$text = "This is a :::test!!!";

Returns true


I tried converting this into Java:

if (Pattern.matches("(?i)\\b(" + word + ")\\b", text)) {
   return true;
}

The same example :

String word = "test";
String text = "This is a :::test!!!";

would return false


What am I missing here? :(

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Lisa Cleaver
  • 213
  • 2
  • 10

1 Answers1

4

You have to use Matcher and call find like this :

Pattern pattern = Pattern.compile("(?i)\\b(" + word + ")\\b");
Matcher matcher = pattern.matcher(text);
System.out.println(matcher.find());// true if match false if not
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140