Help with a regex pattern for finding addition equations that includes both negative numbers and decimals? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Help with a regex pattern for finding addition equations that includes both negative numbers and decimals?

I'm working on a basic Javascript calculator and everything runs as should, but I'm getting hung up on including both decimals and negative numbers in my regex. When testing, I can only match if both sides of the equation have at least 2 digits. Below is my regex, any insight would be greatly appreciated. /-?\d+\.?\d[\d+]?[\+\-] -?\d+\.?\d+

22nd Jan 2018, 7:36 AM
Jeremy
2 Answers
+ 2
Thanks for the response, it seemed to work in testing but still getting the same answer in my code So I'm assuming there's a problem somewhere else. Thanks again for the help, though!
22nd Jan 2018, 8:51 PM
Jeremy
+ 1
(\-?)(\d+)(\.\d+)?([\+|\-])(\-?)(\d+)(\.\d+)? i tested this one which works for equations like A+B and A-B where A,B e R. groups: 0: whole equation 1: sign of A 2: integer part of A 3: float part of A (containing the dot) 4: operator 5: sign of B 6: integer part of B 7: float part of B (containing the dot) hope this helps / is what you needed ps: great name ;)
22nd Jan 2018, 8:09 AM
Jeremy
Jeremy - avatar