简体   繁体   中英

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?

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

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

这个怎么样?

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

为了避免部分匹配,请使用以下命令: '^[0-9]*$'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM