Whitespace at the end of line is not ignored by python regex
<p>The leading spaces are ignored but the trailing ones are not in the below regular expression code. It's just a <code>"Name = Value"</code> string but with spaces. I thought the <code>\s*</code> after the capture would ignore spaces.</p>

<pre><code>import re
line = "  Name =  Peppa Pig  "
match = re.search(r"\s*(Name)\s*=\s*(.+)\s*", line)
print(match.groups())
&gt;&gt;&gt;('Name', 'Peppa Pig   ')  # Why extra spaces after Pig!
</code></pre>

<p>What am I missing?</p>

<p>You're getting trailing spaces because of greedy nature of <code>.+</code>.</p>

<p>You can use this regex to correctly capture your value:</p>

<pre><code>&gt;&gt;&gt; re.search(r"\s*(Name)\s*=\s*(.+?)\s*$", line).groups()
('Name', 'Peppa Pig')
</code></pre>

<p><code>\s*$</code> ensures we are capturing value before trailing white spaces at end.</p>

python regex