Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在寻找一个正则表达式,它将匹配 1 到 n 次出现的数字并且只出现一次算术运算符(只允许 + 或 - )
例如,它应该匹配 -123 或 123- 或 +123 或 123+
这是我到目前为止所拥有的
import re number = "-123" if re.findall(r"[0-9]+[+|-]?", number): return True else: return False
尝试这样的事情:
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+']