Complex regex with custom formula builder
I have a string which is like this - MVAL("A","01-01-1900")+MVAL(B,"01-01-1900")+MVAL("C")+MVAL(D)
. Now I want to extract B
AND D
out of this using regex because it is the first parameter and it has no quotes around it in both the overloaded version of the functions. Secondly because MVAL
function is an overloaded function with two versions like MVAL("A")
and MVAL(B,"01-01-1900")
how will I find which version of the function is being used.
Please help. I'm using System.Text.RegularExpressions.Regex
met开发者_运维技巧hod.
Is it safe to assume that there will never be a comma after the first parameter unless it's followed by a second parameter? If so, this should be all you need:
string s = @"MVAL(""A"",""01-01-1900"")+MVAL(B,""01-01-1900"")+MVAL(""C"")+MVAL(D)";
foreach (Match m in Regex.Matches(s, @"MVAL\((\w+)(,)?"))
{
Console.WriteLine("First param: {0}\nHas second param? {1}\n",
m.Groups[1], m.Groups[2].Success);
}
output:
First param: B
Has second param? True
First param: D
Has second param? False
If there's no comma, the overall match will still succeed because the comma is optional. But, because the second capturing group didn't participate in the match, its Success
property is set to False
.
This regex also assumes there will never be any whitespace within the string, as in your example. This regex allows for whitespace between syntax elements:
@"MVAL\s*\(\s*(\w+)\s*(,)?
It looks like you simply want to match the text MVAL(
followed by a letter (or perhaps an identifier). Try this one:
MVAL\(([A-Z])
The first part, MVAL\(
, matches the prefix. Then we have some text surrounded by parentheses: ([A-Z])
. The parens tell the regular expression engine to "capture" any text the contents will match, which means we can use it later. This is why we had to escape the opening one with a backslash in the prefix.
The [A-Z]
pattern matches any character between A
and Z
. This includes all uppercase alphabetic characters. We then tell the regex engine to ignore the case, so it matches all lowercase characters too.
Dim regex = new Regex("MVAL\(([A-Z])", RegexOptions.IgnoreCase)
Dim match = regex.Match(input)
Dim parameter = match.Groups(1)
If you want to match any valid identifier rather than just a single letter, try this instead:
MVAL\(([A-Z_][A-Z0-9_]*)
That captured part will match any letter or an underscore, followed by zero or more (denoted by the *
) letters, numbers or underscores.
精彩评论