[英]Regular expression to match number(s) and arithmetic operators
I'm looking for a regular expression that would match 1 to n occurrences of numbers and only one occurrence of an arithmetic operator (only + or - are allowed) 我正在寻找一个正则表达式,该表达式将匹配1到n次出现的数字,并且只出现一次算术运算符(仅允许+或-)
For example, it should match -123 or 123- or +123 or 123+ 例如,它应匹配-123或123-或+123或123+
This is what I have so far 这就是我到目前为止
import re
number = "-123"
if re.findall(r"[0-9]+[+|-]?", number):
return True
else:
return False
try something like this: 尝试这样的事情:
In [55]: strs
Out[55]: '+123 abc 123 -123 123- 123+ 14 foo bar'
#you need to escape '+'and '-' In order to search them
In [56]: re.findall(r"\d+[\+|\-]{1}|[\-|\+]{1}\d+",strs)
Out[56]: ['+123', '-123', '123-', '123+']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.