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>Instead of using <code>(.+)\s*</code> <em>(where the <code>\s*</code> is useless since "zero or more white-spaces" isn't a constraint after the greedy quantifier <code>.+</code>, it's like to write nothing)</em>, you can use <code>(.*\S)</code> that will trim automatically the string after the last non-whitespace character <code>\S</code>.</p>

<pre><code>match = re.search(r"\b(Name)\s*=\s*(.*\S)", line)
</code></pre>

<p>Question: is the capture of the already known "Name" literal string really needed?</p>

python regex