Python 整数和整数的正则表达式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/16774064/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regular Expression for whole numbers and integers?
提问by Sahil Thapar
I am trying to detect all integers and whole numbers (among a lot of other things) from a string. Here are the regular expressions I am currently using:
我正在尝试从字符串中检测所有整数和整数(以及许多其他内容)。以下是我目前使用的正则表达式:
Whole numbers: r"[0-9]+"
整数: r"[0-9]+"
Integers: r"[+,-]?[0-9]+"
整数: r"[+,-]?[0-9]+"
Here are the issues:
以下是问题:
- The whole numbers regex is detecting negative numbers as well, which I cannot have. How do I solve this? If I use a space before at start of the regex I get only positive numbers, but then I get a space at the start of my output!
- For whole numbers, I would like to detect positive numbers with the format +[0-9]but store them without the sign.
- For integers, I would like to store any positive integer detected with the sign, irrespective if it is present in the original string.
- 整数正则表达式也在检测负数,这是我不能拥有的。我该如何解决这个问题?如果我在正则表达式开始前使用空格,我只会得到正数,但随后我会在输出开始时得到一个空格!
- 对于整数,我想检测具有格式的正数,+[0-9]但不带符号存储它们。
- 对于整数,我想存储任何用符号检测到的正整数,无论它是否存在于原始字符串中。
Almost done now: One last thing, I have a string that says "Add 10 and -15". I want to store the integers in a list. I do so using the findall(). While storing the numbers is it possible to store '10' as '+10'
现在差不多完成了:最后一件事,我有一个字符串,上面写着“加 10 和 -15”。我想将整数存储在列表中。我使用 findall() 这样做。存储数字时,可以将“10”存储为“+10”
采纳答案by Tim Pietzcker
For positive integers, use
对于正整数,使用
r"(?<![-.])\b[0-9]+\b(?!\.[0-9])"
Explanation:
解释:
(?<![-.])   # Assert that the previous character isn't a minus sign or a dot.
\b          # Anchor the match to the start of a number.
[0-9]+      # Match a number.
\b          # Anchor the match to the end of the number.
(?!\.[0-9]) # Assert that no decimal part follows.
For signed/unsigned integers, use
对于有符号/无符号整数,请使用
r"[+-]?(?<!\.)\b[0-9]+\b(?!\.[0-9])"
The word boundaries \bare crucial to make sure that the entire number is matched.
单词边界\b对于确保整个数字匹配至关重要。
回答by Inbar Rose
You almost had it.
你几乎拥有它。
import re
regex = re.compile(r'(\d+)|([\+-]?\d+)')
s = "1 2 3 4 5 6 +1 +2 +3 -1 -2 -3 +654 -789 321"
for r in regex.findall(s):
    if r[0]:
        # whole (unsigned)
        print 'whole', r[0]
    elif r[1]:
        # a signed integer
        print 'signed', r[1]
Results:
结果:
>>> 
whole 1
whole 2
whole 3
whole 4
whole 5
whole 6
signed +1
signed +2
signed +3
signed -1
signed -2
signed -3
signed +654
signed -789
whole 321
Or, you could use "or" to get the actual result in a "nicer" way:
或者,您可以使用“或”以“更好”的方式获得实际结果:
print [r[0] or r[1] for r in regex.findall(s)]
>>> 
['1', '2', '3', '4', '5', '6', '+1', '+2', '+3', '-1', '-2', '-3', '+654', '-789', '321']
Edit: As per your question " is it possible to store '10' as '+10'" :
编辑:根据您的问题“是否可以将 '10' 存储为 '+10'”:
import re
def _sign(num):
    if r[0]:
        return '+%s'%r[0]
    else:
        return r[1]
regex = re.compile(r'(\d+)|([\+-]?\d+)')
s = "1 2 3 4 5 6 +1 +2 +3 -1 -2 -3 +654 -789 321"      
print [_sign(r) for r in regex.findall(s)]
>>>
['+1', '+2', '+3', '+4', '+5', '+6', '+1', '+2', '+3', '-1', '-2', '-3', '+654', '-789', '+321']
Or in 1-line:
或在 1 行中:
print ['+%s'%r[0] if r[0] else r[1] for r in regex.findall(s)]
>>> 
['+1', '+2', '+3', '+4', '+5', '+6', '+1', '+2', '+3', '-1', '-2', '-3', '+654', '-789', '+321']

