Mike
Mike

Reputation: 2396

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?

Upvotes: 1

Views: 221

Answers (2)

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

Correct this:

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

Upvotes: 0

Barry Kaye
Barry Kaye

Reputation: 7759

Escape the first bracket if it is your intention to match the character ( literally:

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

Upvotes: 8

Related Questions