正则表达困扰C#

时间:2012-12-21 13:53:09

标签: c# .net windows

尝试使用以下正则表达式时遇到问题:

string profileConfig = File.ReadAllText(str);

string startIndex = "user_pref(\"network.proxy.autoconfig_url\", \"";
string endIndex = "\"";

var regex = startIndex + "(.*)" + endIndex;
// Here we call Regex.Match.
Match match = Regex.Match(profileConfig, 
                          regex,
                          RegexOptions.IgnoreCase);

// Here we check the Match instance.
if (match.Success)
{
    // Finally, we get the Group value and display it.
    string key = match.Groups[1].Value;
    MessageBox.Show(key);
}

我收到错误:

  

其他信息:解析   “user_pref(”network.proxy.autoconfig_url“,”(。*)“” - 还不够“。

我的正则表达式在某种程度上是否格式错误?

2 个答案:

答案 0 :(得分:8)

如果您希望字面匹配字符(,请转义第一个括号:

string startIndex = "user_pref\\(\"network.proxy.autoconfig_url\", \"";

答案 1 :(得分:0)

纠正这个:

"user_pref(\"network. -> "user_pref\(\"network.
                                   ^ 
相关问题