使用 RE 从二次方程中提取所有数字
Pulling all numbers from a quadratic equation using RE
我有下面的二次方程,我希望从中将常量拉入元组以找到因子。
3*(x**2)+5*x+6
-(我也不想拉电2)
我尝试了以下表达式。他们中的大多数人似乎正在回归 None
re.search('(\d*),(\d*),(\d*)','3*(x**2)+5*x+6').groups() - returns None
re.findall('([0-9]+.*)+([0-9]+.*)+([0-9]+.*)','3*(x**2)+5*x+6') - returns None
re.split('\D','3*(x**2)+5*x+6') - this is the closest i got - returns - ['3', '', '', '', '', '2', '', '5', '', '', '6']
有什么想法吗?我更愿意使用 re
而不是任何其他模块。
如果你只想要常量,你可以简单地使用 negative look-behind :
>>> re.findall(r'(?<!\*\*)\d',s)
['3', '5', '6']
r'(?<!\*\*)\d'
将匹配前面没有双 *
字符的任何数字。
我有下面的二次方程,我希望从中将常量拉入元组以找到因子。
3*(x**2)+5*x+6
-(我也不想拉电2)
我尝试了以下表达式。他们中的大多数人似乎正在回归 None
re.search('(\d*),(\d*),(\d*)','3*(x**2)+5*x+6').groups() - returns None
re.findall('([0-9]+.*)+([0-9]+.*)+([0-9]+.*)','3*(x**2)+5*x+6') - returns None
re.split('\D','3*(x**2)+5*x+6') - this is the closest i got - returns - ['3', '', '', '', '', '2', '', '5', '', '', '6']
有什么想法吗?我更愿意使用 re
而不是任何其他模块。
如果你只想要常量,你可以简单地使用 negative look-behind :
>>> re.findall(r'(?<!\*\*)\d',s)
['3', '5', '6']
r'(?<!\*\*)\d'
将匹配前面没有双 *
字符的任何数字。