Search code examples
regexpattern-matchingexpressionmathematical-expressions

Pattern matching of `space` in mathematical expressions


I want a pattern which can match both of these expressions:

expr1 = `a + b` 
expr1 = `a+b`

Thanks.


Solution

  • try this:

    `(\w+)\s*(\S)\s*(\w+)`

    first hit gets you the first variable (a), second hit gets you the operand (+) and third hit gets you the second variable (b)
    Works also for python
    Edit:
    resowing the backticks and here's a regex checker for you: https://regex101.com/r/Hy1vSl/1