Search code examples
regexvb.netstring.net-2.0

Unable to figure out regex pattern


I have a string with some custom formula like FVAL("A")+FVAL(B). I want to figure out all string inside FVAL() which does not have quotes around it.

So basically I want to extract out B because it does not have quotes around it.


Solution

  • Use

    FVAL\(([^")]*)\)
    

    This matches FVAL(, followed by any number of characters except quotes or closing parentheses, followed by ).

    Another possibility (where the match would be in $0 instead of $1:

    (?<=FVAL\()[^")]*(?=\))
    

    This matches a non-quote/non-parenthesis-string that is surrounded by FVAL( and ).

    In VB.net:

    Dim RegexObj As New Regex("FVAL\(([^"")]*)\)", RegexOptions.IgnoreCase)
    Dim MatchResult As Match = RegexObj.Match(SubjectString)
    While MatchResult.Success
        ResultList.Add(MatchResult.Groups(1).Value)
        MatchResult = MatchResult.NextMatch()
    End While
    

    or

    Dim RegexObj As New Regex("(?<=FVAL\()[^"")]*(?=\))", RegexOptions.IgnoreCase)
    Dim MatchResult As Match = RegexObj.Match(SubjectString)
    While MatchResult.Success
        ResultList.Add(MatchResult.Value)
        MatchResult = MatchResult.NextMatch()
    End While