1

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

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", "(.*)"" - 还不够)。

我的正则表达式是否以某种方式格式错误?

4

2 回答 2

8

如果您打算(从字面上匹配字符,请转义第一个括号:

string startIndex = "user_pref\\(\"network.proxy.autoconfig_url\", \"";
于 2012-12-21T13:54:52.503 回答
0

纠正这个:

"user_pref(\"network. -> "user_pref\(\"network.
                                   ^ 
于 2012-12-21T13:56:40.643 回答