Soham Dasgupta
Soham Dasgupta

Reputation: 5199

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.

Upvotes: 1

Views: 120

Answers (2)

Tim Pietzcker
Tim Pietzcker

Reputation: 336468

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

Upvotes: 1

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81724

You'll want an expression something like

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

which includes a set of parentheses for the capture group.

Upvotes: 0

Related Questions