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 '3x3+6x2+2x1+8x0' I need to get ['3', '6', '2', '8'] and ['3', '2', '1', '0']
-
I am just added the signs to the coefficients...if there is a + then I ignore it, and the coefficient right next to it is left positive. If there is a - then I pick it up by just adding a -* just before the \d, it works just fine.user35288– user352882009-05-31 12:50:03 +00:00Commented May 31, 2009 at 12:50
3 Answers
You can use positive look-ahead to match something that is followed by something else. To match the coefficients, you can use:
>>> s = '3x3+6x2+2x1+8x0'
>>> re.findall(r'\d+(?=x)', s)
['3', '6', '2', '8']
From the documentation of the re module:
(?=...) Matches if ... matches next, but doesn’t consume any of the string. This is called a lookahead assertion. For example, Isaac (?=Asimov) will match 'Isaac ' only if it’s followed by 'Asimov'.
For the exponents, you can use positive look-behind instead:
>>> s = '3x3+6x2+2x1+8x0'
>>> re.findall(r'(?<=x)\d+', s)
['3', '2', '1', '0']
Again, from the docs:
(?<=...) Matches if the current position in the string is preceded by a match for ... that ends at the current position. This is called a positive lookbehind assertion. (?<=abc)def will find a match in abcdef, since the lookbehind will back up 3 characters and check if the contained pattern matches.