简体   繁体   中英

Regular expression woes C#

I am having issues when attempting to use the following regular expression:

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);
}

I get the error:

Additional information: parsing "user_pref("network.proxy.autoconfig_url", "(.*)"" - Not enough )'s.

Is my regular expression malformed in some way?

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

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

Correct this:

"user_pref(\"network. -> "user_pref\(\"network.
                                   ^ 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM