- string strRegexConn = @"((?:server=(?<server>[^;\(\)]+)[;]).*(?:provider=(?<provider>[^;]+);).*(?:host=(?<host>[\w\d\.]+)).*(?:port=(?<port>\d+)).*(?:service_name=(?<servicename>[^\(\)]+)).*(?:user\sid=(?<userid>[^;]+)).*(?:password=(?<password>[^;]+)))|(?:provider=(?<provider>[^;]+);).*?(?:server=(?<server>[^;\(\)]+)[;]).*?(?:database=(?<database>[^;]+).*?(?:user\sid=(?<userid>[^;]+)).*(?:password=(?<password>[^;]+)))";
- string str="provider=Microsoft Sql Server 2000;server=172.16.10.100;database=ObxMetabaseHDDemo;User ID=sa;Password=123;MultipleActiveResultSets=True";
- Regex r = new Regex(strRegexConn, RegexOptions.IgnoreCase);
- Match m = r.Match(str);
- string provider = m.Groups["provider"].Value;
- string server= m.Groups["server"].Value;
备注:保证正则表达式是在同一行不然运行无法通过