I am trying to find word
s="the the the the"
output - [" the "]
The problem here is that
\s patterns consume the whitespace. The
scan method only matches non-overlapping matches, and your expected matches are overlapping.
You need to use looakrounds to get overlapping matches:
(?<=\s)- a positive lookbehind that requires a whitespace to be present immediately before the
the- a literal text
(?=\s)- a positive lookahead that requires a whitespace right after the
Note that if you use
\bthe\b (i.e. use word boundaries), you will get all
the instances from your string as
\b just asserts the position before or after a word char (letter, digit or underscore).