Henok Yemane
Henok Yemane

Reputation: 11

How can i solve this regular expression, Python?

I would like to construct a reg expression pattern for the following string, and use Python to extract:

str = "hello w0rld how 34 ar3 44 you\n welcome 200 stack000verflow\n"

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

import re
x = re.findall('([0-9]+)', str)

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

Upvotes: 0

Views: 71

Answers (5)

Amin Etesamian
Amin Etesamian

Reputation: 3699

To avoid a partial match use this: '^[0-9]*$'

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336148

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 word boundary anchors:

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))) 

Result: 297

Upvotes: 0

LycuiD
LycuiD

Reputation: 2575

s = re.findall(r"\s\d+\s", a)  # \s matches blank spaces before and after the number.
print (sum(map(int, s)))       # print sum of all

\d+ matches all digits. This gives the exact expected output.

278

Upvotes: 1

ham
ham

Reputation: 716

Why not try something simpler like this?:

str = "hello w0rld how 34 ar3 44 you\n welcome 200 stack000verflow\n"
print sum([int(s) for s in str.split() if s.isdigit()])
# 278

Upvotes: 1

Enix
Enix

Reputation: 4579

How about this?

x = re.findall('\s([0-9]+)\s', str)

Upvotes: 0

Related Questions