I need a regex expression to split math expression, like:
-1*x+2*y
to the: -1*x and 2*y
I can’t just split that string basing in the + and - (because there can be negative numbers)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can split on
+or-which is preceded by a letter, digit and potentially other elements that are acceptable in your expressions (parenthesis for example). You can use look-behind to accomplish this.For example in python you can do it like this:
This splits on every
+or-not preceded by a small or capital letter or a digit or a closing parenthesis. You may need to tweak this depending on your exact specification of what constitutes a “math expression”.This or similar regular expression should work in other regexp engines.