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+)(.*)(?=\)) 

see @ regex101

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

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -