Split to parts:
string regexSplitToParts = "([^=;]*)=([^=;]*)"; RegexOptions options = RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.Compiled; Regex reg = new Regex(regexSplitToParts, options);
Fetch specific part of connection string (e.g. DataSource):
string regexDataSource = "Data\\sSource(\\s)*=(\\s)*(?<DataSourceName>([^;]*))"; RegexOptions options = RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.Compiled; Regex reg = new Regex(regexDataSource, options);
Technorati Tags: regex
2 comments:
Thanks very much - this is very useful
Maybe overkill... (.NET Flavor) - but I do get the tokens back with named captures so I can work with specific keys or values. That being said, parsing ConnectionStrings with the DbConnectionStringBuilder class is way easier. Do note that for that class, if a token repeats, apparently only the last one is kept - so User ID =x ; User ID =y will store User Id=y (at least, this is the case for Oracle)
[ ]*(?(?(?![ ])(?:[A-Za-z0-9\x20\x5f-[\x3b\x3d]]*)*(?(?![ ])=(?(?![ ])[^=;]*(?(?![ ]);(?<![ ]))(?<![ ]))
for
Data Source = <Host>:<Port> / <SID>; Persist Security Info = True; Statement Cache Size = 200; User ID=<SomebodyReal>; Password = <TheirRealPassword>;proxy user id = <AppUser>;proxy password =<AppUserPwd>;
Gives back:
Data Source = <Host>:<Port> / <SID>;
Persist Security Info = True;
Statement Cache Size = 200;
User ID=<SomebodyReal>;
Password = <TheirPassword>;
proxy user id = <AppUser>;
proxy password = <AppUserPwd>;
Post a Comment