How can i solve this regular expression, Python?
<p>I would like to construct a reg expression pattern for the following string, and use Python to extract:</p>

<pre><code>str = "hello w0rld how 34 ar3 44 you\n welcome 200 stack000verflow\n"
</code></pre>

<p>What I want to do is extract the <strong>independent</strong> number values and add them which should be 278. A prelimenary python code is:</p>

<pre><code>import re
x = re.findall('([0-9]+)', str)
</code></pre>

<p>The problem with the above code is that numbers within a char substring like 'ar3' would show up. Any idea how to solve this?</p>

<p>The solutions posted so far only work (if at all) for numbers that are preceded and followed by whitespace. They will fail if a number occurs at the very start or end of the string, or if a number appears at the end of a sentence, for example. This can be avoided using <a href="http://www.regular-expressions.info/wordboundaries.html" rel="nofollow">word boundary anchors</a>:</p>

<pre><code>s = "100 bottles of beer on the wall (ignore the 1000s!), now 99, now only 98"
s = re.findall(r"\b\d+\b", a)  # \b matches at the start/end of an alphanumeric sequence
print(sum(map(int, s))) 
</code></pre>

<p>Result: <code>297</code></p>

python regex