I don’t know if that’s the right word for it, but I am trying to come up with some regex’s that can extract coefficients and exponents from a mathematical expression. The expression will come in the form ‘axB+cxD+exF’ where the lower case letters are the coefficients and the uppercase letters are the exponents. I have a regex that can match to both of them, but I’m wondering if I can use 2 regexs, one to match the coefficients and one for the exponents. Is there a way to match a number with a letter on one side of it without matching the letter? EG, in ‘3×3+6×2+2×1+8×0’ I need to get
[‘3’, ‘6’, ‘2’, ‘8’]
and
[‘3’, ‘2’, ‘1’, ‘0’]
I don’t know if that’s the right word for it, but I am trying
Share
You can use positive look-ahead to match something that is followed by something else. To match the coefficients, you can use:
From the documentation of the
remodule:For the exponents, you can use positive look-behind instead:
Again, from the docs: