Ifedee Ifedee - 1 month ago 10
Perl Question

How can I extract variables with nonzero coefficients from an equation using a Perl regex?

I wish to grab the non-zero coefficients of a particular number arrangement. I have the set of numbers below and I wish to strictly extract with regex the non-zero coefficient (

x2
,
x3
,
x5
) from

x1 *0.000000+ x2 *-0.100000+ x3 *0.850000+ x4 *0.000000+ x5 *0.000056+ x6 *0.000000


I tried doing this

(\*[-+]?[0]*\.?[0]+)


but am not really sure as to declare that it shouldn't consider the
"*0.000000"

Answer

Match x-digit not followed by space-star-zero-dot-zeroes-endOfWord:

x\d+(?! \*0\.0+\b)

See live demo.