Search code examples
regexvb.netstring.net-2.0

Correctness of a regular expression


I'm trying to create a regex which will match either one of the following -

FVAL(A)
FVAL("A")
FVAL(A,B)
FVAL("A",B)
FVAL("A","B")
FVAL(A,"B")
FVAL(A,B,C)
FVAL("A",B,C)
FVAL("A","B",C)
FVAL("A","B","C")
FVAL("A",B,"C")
FVAL(A,"B","C")

Regex -

FVAL\s*\(\s*["*]\s*\w+\s*["*]\s*,*\s*["*]\s*\w+\s*["*]\s*,*\s*,*\s*["*]\s*\w+\s*["*]\s*\)

This regex is supposed to return all and any form of the function that is used.

For e.g. -
If match string were - FVAL(A,"B")+5 then match group should be FVAL(A,"B")

P.S. - I'm ignoring white spaces in match string, but they can be there.


Solution

  • Your expression is way too complicated.

    FVAL\("?\w+"?(?:,"?\w+"?){0,2}\)
    

    Breakdown:

    FVAL         # "FVAL"
    \(           # "("
    "?           # an optional double quote
    \w+          # at least one word character
    "?           # an optional double quote
    (?:          # group
      ,          #   a comma
      "?\w+"?    #   quote - word character - quote
    ){0,2}       # end group, repeat 0-2 times
    \)           # ")"
    

    Insert whitespace \s into the expression where you see fit.