繁体   English   中英

如何解决这个正则表达式,Python?

[英]How can i solve this regular expression, Python?

我想为以下字符串构造一个reg表达式模式,并使用Python提取:

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

我想要做的是提取独立的数字值,并将其相加,它们应该是278。初步的python代码是:

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

上面的代码的问题在于,像“ ar3”这样的char子字符串中的数字会显示出来。 任何想法如何解决这个问题?

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+匹配所有数字。 这给出了确切的预期输出。

278

为什么不尝试这样简单的事情呢?:

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]*$'

到目前为止发布的解决方案仅适用于(如果有的话)空格之前和之后的数字。 例如,如果数字出现在字符串的开头或结尾,或者如果数字出现在句子的末尾,则它们将失败。 使用单词边界锚可以避免这种情况:

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

结果: 297

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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