how to write regular expression to extract content of metadata, provider, provider connection string from
metadata=res://*/ent.csdl|res://*/ent.ssdl|res://*/ent.msl;provider=System.Data.SqlClient;provider connection string="Data Source=1.1.1.1;Initial Catalog={0};Persist Security Info=True;User ID=user;Password=pass;MultipleActiveResultSets=True"
I mean, I want to get
Metadata: res://*/ent.csdl|res://*/ent.ssdl|res://*/ent.msl
Provider: System.Data.SqlClient
provider connection string: Data Source=1.1.1.1;Initial Catalog={0};Persist Security Info=True;User ID=user;Password=pass;MultipleActiveResultSets=True
Without assuming any specific order of the ConnString:
var connString = @"metadata=res://*/ent.csdl|res://*/ent.ssdl|res://*/ent.msl;provider=System.Data.SqlClient;provider connection string=""Data Source=1.1.1.1;Initial Catalog={0};Persist Security Info=True;User ID=user;Password=pass;MultipleActiveResultSets=True""";
Regex metaRegex = new Regex(@"metadata=(?<metadata>[^;]+)");
Regex connRegex = new Regex(@"provider\sconnection\sstring=""(?<conn>[^""]+)");
Regex providerRegex = new Regex(@"provider=(?<provider>[^;]+)");
Console.WriteLine("MetaData: " + metaRegex.Match(connString).Groups["metadata"]);
Console.WriteLine("Connection String: " + connRegex.Match(connString).Groups["conn"]);
Console.WriteLine("Provider: " + providerRegex.Match(connString).Groups["provider"]);
A regex such as:
^(res:[^;]+);provider=([^;]+);provider\sconnection\sstring=\"(.+)\"$
Gives you 3 capture groups, providing the parts you need.
Live example: http://rextester.com/rundotnet?code=JRGSO74516
You can use named capture groups to define patterns for each category, which is supported by .Net RegEx matching. See here for an explanation on named capture groups.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.