[英]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: 我想为以下字符串构造一个reg表达式模式,并使用Python提取:
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: 我想要做的是提取独立的数字值,并将其相加,它们应该是278。初步的python代码是:
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. 上面的代码的问题在于,像“ ar3”这样的char子字符串中的数字会显示出来。 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. \\d+
匹配所有数字。 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
结果:
297
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.