I want to convert my eq to vector form so i need to extract the coefficient
r"[-+]?\d*\.\d+|\d+"
I tried this but I am not able to get the "-" sign along with the integer.
For 2x-3y+0 i am getting [2, 3, 0]
but I need [2, -3, 0]
.
I want to convert my eq to vector form so i need to extract the coefficient
r"[-+]?\d*\.\d+|\d+"
I tried this but I am not able to get the "-" sign along with the integer.
For 2x-3y+0 i am getting [2, 3, 0]
but I need [2, -3, 0]
.
The expression in this answer is much better, since it does not capture the
+
for instance.
Being said that, my guess is your designed expression is also just fine, maybe we'd slightly modify that to:
[-+]?\d+\.\d+|[-+]?\d+
and it might likely work, since validation seems to be unnecessary.
import re
matches = re.finditer(r"[-+]?\d+\.\d+|[-+]?\d+", "-0.2x-0.73y-0.11z-0.2x-0.73y-0.11")
linear_eq_coeff=[]
for match in matches:
linear_eq_coeff.append(match.group())
print linear_eq_coeff
['-0.2', '-0.73', '-0.11', '-0.2', '-0.73', '-0.11']
const regex = /[-+]?\d+\.\d+|[-+]?\d+/gm;
const str = `-0.2x-0.73y-0.11z-0.2x-0.73y-0.11`;
let m;
arr = [];
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
arr.push(match);
});
}
console.log(arr);
wjandrea advice is that:
Could simplify to
[-+]?(\d*\.)?\d+
Just use a better regex.
>>>import re
>>>eqn = '2x-3y+0'
>>>re.findall(r'(-?(?:\d+(?:\.\d*)?|\.\d+))', eqn)
['2', '-3', '0']