r'((?:\S+\s+){0,3}\bwhite\b\s*(?:\S+\s+){0,3})
The result of above regex for below sentence is
sentence = This is a white floral garment.
result = This is a white floral
But I want the result as
wanted result = This is a white floral garment
I want 3 words before and after the white word. If there are not three word after white than at least get all which are present there.
\s+
requires at least 1 whitespace (at the end). You may try(?:\S+\s+){0,3}white(?:\s+\S+){0,3}