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? :(

YCF_L
  • 51,266
  • 13
  • 85
  • 129
Lisa Cleaver
  • 183
  • 1
  • 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
YCF_L
  • 51,266
  • 13
  • 85
  • 129