当然。用正则表达式给猫剥皮的方法有很多。见下文。
为了娱乐和教育,这里是这个问题的正则表达式解决方案:
注意我故意只展示如何用正则表达式处理连接字符串,因为 XML 应该用 XmlReader、XDocument、XPathReader 或 XmlDocument(等)处理。
在http://ideone.com/I47R8上现场观看
using System;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
public class Program
{
const string kvPair = @"(?<pair>(?<key>.*?)\s*=\s*(?<value>.*?))";
const string kvpRegex = "^" + kvPair + "$";
const string csRegex = @"^\s*(?<cs>(?:" + kvPair + @"\s*(;\s*|\s*$))+)$";
public static void Main(string[] args)
{
const string input = @"Data Source=Instance1;User ID=;Initial Catalog=DB;Provider=SQLOLEDB.1;Integrated Security=SSPI;Auto Translate=False;";
var match = Regex.Match(input, csRegex);
Console.WriteLine("======\nSuccess: {0}\n======", match.Success);
// 'smart' dictionary from the subcaptures:
var dict = match.Groups["pair"]
.Captures
.Cast<Capture>()
.Select(cap => Regex.Match(cap.Value, kvpRegex))
.ToDictionary(submatch => submatch.Groups["key"].Value,
submatch => submatch.Groups["value"].Value);
foreach (var kvp in dict)
Console.WriteLine(kvp);
// Getting just the DB1:
Console.WriteLine("======\nCatalog: {0}\n======", dict["Initial Catalog"]);
// raw access from csRegex full match:
foreach (Capture cap in match.Groups["key"].Captures)
Console.WriteLine("Key: '{0}'", cap.Value);
foreach (Capture cap in match.Groups["value"].Captures)
Console.WriteLine("Value: '{0}'", cap.Value);
Console.WriteLine("======\nConnection string: '{0}'", match.Groups["cs"].Value);
}
}
使用正确的输出:
======
Success: True
======
[Data Source, Instance1]
[User ID, ]
[Initial Catalog, DB]
[Provider, SQLOLEDB.1]
[Integrated Security, SSPI]
[Auto Translate, False]
======
Catalog: DB
======
Key: 'Data Source'
Key: 'User ID'
Key: 'Initial Catalog'
Key: 'Provider'
Key: 'Integrated Security'
Key: 'Auto Translate'
Value: 'Instance1'
Value: ''
Value: 'DB'
Value: 'SQLOLEDB.1'
Value: 'SSPI'
Value: 'False'
======
Connection string: 'Data Source=Instance1;User ID=;Initial Catalog=DB;Provider=SQLOLEDB.1;Integrated Security=SSPI;Auto Translate=False;'