c#从反向引用中获取值

时间:2012-10-11 21:37:33

标签: c# regex backreference

我正在尝试从XML文件中提取数据库连接字符串,如下所示:

<DTS:Property DTS:Name="ConnectionString">
    Data Source=Instance1;
    User ID=;
    Initial Catalog=DB;
    Provider=SQLOLEDB.1;
    Integrated Security=SSPI;
    Auto Translate=False;    
</DTS:Property>
</DTS:ConnectionManager>
    </DTS:ObjectData>
</DTS:ConnectionManager>
<snip>

理想情况下,我希望有一个小组回来

  

[0] =数据源= Instance_1;用户ID =;初始目录= DB1;提供商= SQLOLEDB.1;集成安全性= SSPI;自动翻译=错误;

     

[1] = DB1

所以我有这个正则表达式:

<DTS:Property DTS:Name="ConnectionString">(?<cs>.*)(?<cs>)</DTS:Property>

所以我在第一个()中捕获了值,有没有办法可以使用正则表达式来捕获完整的连接字符串而只捕获数据库?

1 个答案:

答案 0 :(得分:1)

不确定。有很多方法可以用regexen去皮猫。见下文。

我建议 Connection String Builders


为了娱乐和教育,这里有一个问题的解决方案:

  

注意我故意只显示 如何使用regex处理连接字符串,因为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;'