1

I need to match an exact substring in a string in Java. I've tried with

String pattern = "\\b"+subItem+"\\b";

But it doesn't work if my substring contains non alphanumerical characters. I want this to work exactly as the "Match whole word only" function in Notepad++. Could you help?

Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476

1 Answers1

1

I suggest either unambigous word boundaries (that match a string only if the search pattern is not enclosed with letters, digits or underscores):

String pattern = "(?<!\\w)"+Pattern.quote(subItem)+"(?!\\w)";

where (?<!\w) matches a location not preceded with a word char and (?!\w) fails if there is no word char immediately after the current position (see this regex demo), or, you can use a variation that takes into account leading/trailing special chars of the potential match:

String pattern = "(?:\\B(?!\\w)|\\b(?=\\w))" + Pattern.quote(subword) + "(?:(?<=\\w)\\b|(?<!\\w)\\B)";

See the regex demo.

Details:

  • (?:\B(?!\w)|\b(?=\w)) - either a non-word boundary if the next char is not a word char, or a word boundary if the next char is a word char
  • Data\[3\] - this is a quoted subItem
  • (?:(?<=\w)\b|(?<!\w)\B) - either a word boundary if the preceding char is a word char, or a non-word boundary if the preceding char is not a word char.
Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476