Reputation: 644
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)
Upvotes: 2
Views: 792
Reputation: 626794
If you want to match a whole word TREE
that is not preceded with X-MAS
and a whitespace, you may use a negative lookbehind (?<!X-MAS\\s)
(or, to make sure the X-MAS
is a whole word, (?<!\\bX-MAS\\s)
):
String pat = "\\b(?<!X-MAS\\s)TREE\\b";
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:
String pat = "\\b(?<!X-MAS\\s{1,10})TREE\\b";
Here, even if there are no or up to 10 whitespaces between X-MAS
and TREE
, the negative condition (the so-called constrained-width negative lookbehind) will work.
See this Java demo.
Upvotes: 1
Reputation: 10466
You can try this:
^(?!X-MAS\s+TREE\s*)(?=.*TREE).*$
(?!X-MAS\s+TREE\s*)
(?=.*TREE)
Assert that the Regex below matches .*To cover your comment's structure, you can try negative look behind
\b.*(?<!X-MAS )TREE\b
Upvotes: 2