Python regex with \w does not work
<p>I want to have a regex to find a phrase and two words preceding it if there are two words.
For example I have the string (one sentence per line):</p>

<blockquote>
  <p>Chevy is my car and Rusty is my horse.
  My car is very pretty my dog is red.</p>
</blockquote>

<p>If i use the regex:</p>

<pre><code>re.finditer(r'[\w+\b|^][\w+\b]my car',txt)
</code></pre>

<p>I do not get any match.</p>

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

<pre><code>re.finditer(r'[\S+\s|^][\S+\s]my car',txt)
</code></pre>

<p>I am getting:
's my car' and '. My car' (I am ignoring case and using multi-line)</p>

<p>Why is the regex with \w+\b not finding anything? It should find two words and 'my car'
How can I get two complete words before 'my car' if there are two words. If there is only one word preceding my car, I should get it. If there are no words preceding it I should get only  'my car'.  In my string example I should get: 'Chevy is my car' and 'My car' (no preceding words here)</p>

<p>In your <code>r'[\w+\b|^][\w+\b]my car</code> regex, <code>[\w+\b|^]</code> matches 1 symbol that is either a word char, a <code>+</code>, a backdpace, <code>|</code>, or <code>^</code> and <code>[\w+\b]</code> matches 1 symbol that is either a word char, or <code>+</code>, or a backspace.</p>

<p>The point is that inside a character class, quantifiers and a lot (<strong>but not all</strong>) special characters match literal symbols. E.g. <code>[+]</code> matches a plus symbol, <code>[|^]</code> matches either a <code>|</code> or <code>^</code>. Since you want to match a <em>sequence</em>, you need to provide a sequence of subpatterns outside of a character class.</p>

<p>It seems as if you intended to use <code>\b</code> as a word boundary, however, <code>\b</code> inside a character class matches only a backspace character.</p>

<p>To <em>find two words and 'my car'</em>, you can use, for example</p>

<pre><code>\S+\s+\S+\s+my car
</code></pre>

<p>See the <a href="https://regex101.com/r/eY4wC6/2" rel="nofollow">regex demo</a> (here, <code>\S+</code> matches one or more non-whitespace symbols, and <code>\s+</code> matches 1 or more whitespaces, and the 2 occurrences of these 2 consecutive subpatterns match these symbols as a <em>sequence</em>).</p>

<p>To make the sequences before <code>my car</code> optional, just use a <code>{0,2}</code> quantifier like this:</p>

<pre><code>(?:\S+[ \t]+){0,2}my car
</code></pre>

<p>See <a href="https://regex101.com/r/eY4wC6/3" rel="nofollow">this regex demo</a> (to be used with the <code>re.IGNORECASE</code> flag). See <a href="https://ideone.com/B930oh" rel="nofollow">Python demo</a>:</p>

<pre><code>import re
txt  = 'Chevy is my car and Rusty is my horse.\nMy car is very pretty my dog is red.'
print(re.findall(r'(?:\S+[ \t]+){0,2}my car', txt, re.I))
</code></pre>

<p><strong>Details</strong>:</p>

<ul>
<li><code>(?:\S+[ \t]+){0,2}</code> - 0 to 2 sequences of 1+ non-whitespaces followed with 1+ space or tab symbols (you may also replace it with <code>[^\S\r\n]</code> to match any horizontal space or <code>\s</code> if you also plan to match linebreaks).</li>
<li><code>my car</code> - a literal text <code>my car</code>.</li>
</ul>

python regex python-3.x