1

I am trying to figure out a regex that will match a certain word, where the word cannot be a part of any other "word".

So this word that I am trying to match can only have spaces, tabs or linebreaks in front or after it.

I have tried the following:

s\sWORD$
s\sWORD\s
^WORD\s
anubhava
  • 761,203
  • 64
  • 569
  • 643
osk
  • 790
  • 2
  • 10
  • 31

1 Answers1

4

this word that I am trying to match can only have spaces, tabs or linebreaks in front or after it.

One of these regex patterns should work for you:

(?<=\s|^)WORD(?=\s|$)
(?<!\S|^)WORD(?!\S)

First one means WORD must be preceded by whitespace or line start and it must be followed by whitespace or line end.

Second one means WORD must NOT be preceded by non-whitespace and it must NOT be followed by a non-whitespace.

Java examples:

"WORD abc".matches(".*?(?<=\\s|^)WORD(?=\\s|$).*"); // true

"WORD".matches(".*?(?<=\\s|^)WORD(?=\\s|$).*"); // true

"WORD-abc".matches(".*?(?<=\\s|^)WORD(?=\\s|$).*"); // false

"some-WORD".matches(".*?(?<=\\s|^)WORD(?=\\s|$).*"); // false
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Actually I also didn't post an answer for a long time thinking all OP needs is word boundary. But then I re-read question and found this quoted line **this word that I am trying to match can only have spaces, tabs or linebreaks in front or after it.** and with that I concluded that `WORD-abc` should not match as the case with `\b`. Hence the need of look-arounds to make sure word is only surrounded by space or start/end anchors. – anubhava May 20 '15 at 18:25
  • 1
    Ok, then `\b` would not work. Good answer! – hek2mgl May 21 '15 at 18:26