Please consider the following text :
That is, it matches at any position that has a non-word character to the left of it, and a word character to the right of it.
How can I get the following result :
That is, it matches at any position that has a non-word character to the
That is everything until left
^anchors to the beginning of the string.*?matches as little as possible of any character\bmatches a word boundaryleftmatches the string literal"left".*matches the remainder of the string$anchors to the end of the string$1replaces the matched string with group 1 in()If you want to use any word (not just
"left"), be careful to escape it. You can usePattern.quote(word)to escape the string.