Python 3 regex word boundary unclear
<p>I am using a regex to find the string 'my car' and detect up to four words before it. My reference text is:</p>

<pre><code>my house is painted white, my car is red.
A horse is galloping very fast in the road, I drive my car slowly.
</code></pre>

<p>if I use the regex:</p>

<pre><code>re.finditer(r'(?:\w+[ \t,]+){0,4}my car',txt,re.IGNORECASE|re.MULTILINE)
</code></pre>

<p>I am getting the expected results.For example: house is painted white, my car</p>

<p>if I use the regex:</p>

<pre><code>re.finditer(r'(?:\w+\b){0,4}my car',txt,re.IGNORECASE|re.MULTILINE)
</code></pre>

<p>I am getting only: 'my car' and 'my car'
That is, I am not getting up to four words before it.
Why I cannot use the \b to match the words in the group {0,4}?</p>

<p>You could use:</p>

<pre><code>(?:\b\w+\W+){4}
\b(?:my\ car)\b
</code></pre>

<p>See <a href="https://regex101.com/r/iB6xL0/3" rel="nofollow"><strong>a demo on regex101.com</strong></a>.<hr>
In <code>Python</code> this will be:</p>

<pre><code>import re

rx = re.compile(r'''
                (?:\b\w+\W+){0,4}
                \b(?:my\ car)\b
                ''', re.VERBOSE)

string = """
my house is painted white, my car is red.
A horse is galloping very fast in the road, I drive my car slowly.
"""
words = rx.findall(string)
print(words)
# ['house is painted white, my car', 'the road, I drive my car']
</code></pre>

python regex python-3.x