Regex for matching Functions and Capturing their Arguments

后端 未结 5 839
自闭症患者
自闭症患者 2020-12-02 17:37

I\'m working on a calculator and it takes string expressions and evaluates them. I have a function that searches the expression for math functions using Regex, retrieves the

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 18:11

    This regex does what you want:

    ^(?\w+)\((?>(?(param),)(?(?>(?>[^\(\),"]|(?

    \()|(?<-p>\))|(?(p)[^\(\)]|(?!))|(?(g)(?:""|[^"]|(?<-g>"))|(?!))|(?")))*))+\)$

    Don't forget to escape backslashes and double quotes when pasting it in your code.

    It will match correctly arguments in double quotes, inner functions and numbers like this one:
    f1(123,"df""j"" , dhf",abc12,func2(),func(123,a>2))

    The param stack will contains
    123
    "df""j"" , dhf"
    abc12
    func2()
    func(123,a>2)

提交回复
热议问题