C# getting variables from a CAST statement using Regex -
i have following input string:
string text = "cast(@myvar datetime)";
is there way extract variable , datatype used using regex?
expected output:
name ='@myvar' type ='datetime'
you use following expression , use capturing groups pairs:
(?<=cast\()(.*)(?:\s+as\s+)(.*)(?=\))
var text = "cast(@myvar1 datetime2)" + environment.newline + "cast(@myvar2 datetime2)"; var matches = new regex(@"(?<=cast\()(.*?)(?:\s+as\s+)(.*?)(?=\))", regexoptions.ignorecase).matches(text); foreach(match match in matches) { // first capturing group full match, need start index 1 actual group 1 var name = match.groups[1]; var type = match.groups[2]; console.writeline("name: {0}, type: {1}", name, type); } // name: @myvar1, type: datetime2 // name: @myvar2, type: datetime2
Comments
Post a Comment