I have a problem with excluding some special condition. I already create example in this LINK.
If I have List of Sentence like this :
X-MAS TREE //it should be excluded because match my dictionary
BLA BLA TREE
XMAS TREE
X-MASTREE
X-TREE
X-MASTREE
And I have Dictionary that X-MAS TREE has synonyms: XMAS TREE, X-MASTREE, X-TREE, TREE. And I need to change all the synonyms word into my Dictionary Word.
How to exclude X-MAS TREE? Because all of those regex, will be replace with X-MAS TREE If I search with keyword TREE, it will be infinite loop, because X-MAS TREE has TREE
I already tried many combination, but is not working:
\b(XMAS TREE|X\-MASTREE|X\-TREE|TREE|(?!X\-MAS TREE)\b
\b(XMAS TREE|X\-MASTREE|X\-TREE|(?!X\-MAS \s)TREE)\b
\b(XMAS TREE|X\-MASTREE|X\-TREE|((?!X\-MAS )|\w*)TREE)\b
\b(XMAS TREE|X\-MASTREE|X\-TREE|(?:(?!X\-MAS) )TREE)\b
EDIT
I need to use Boundaries (for some reason), because I create the regex in my code, with looping, and need to use it for another Dictionary, that why, for this case, I need special condition (without change the structure code, only edit the regex TREE)
If you want to match a whole word
TREE
that is not preceded withX-MAS
and a whitespace, you may use a negative lookbehind(?<!X-MAS\\s)
(or, to make sure theX-MAS
is a whole word,(?<!\\bX-MAS\\s)
):See the regex demo.
Also, if there can be more than 1 whitespace, say, from 1 up to 10, you may add a limiting quantifier
{1,10}
after\s
to make sure more than 1 whitespace is still accounted for:Here, even if there are no or up to 10 whitespaces between
X-MAS
andTREE
, the negative condition (the so-called constrained-width negative lookbehind) will work.See this Java demo.