Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正则表达式#_C#_.net_Windows - Fatal编程技术网

C# 正则表达式#

C# 正则表达式#,c#,.net,windows,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 = Reg

尝试使用以下正则表达式时出现问题:

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);
}
我得到一个错误:

附加信息:解析 “用户前缀(“network.proxy.autoconfig\u url”、“(.*”)-不够)”的


我的正则表达式是否存在某种形式的错误?

如果您想匹配字符
字面意思为:

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

如果要匹配字符
字面意思为:

string startIndex = "user_pref\\(\"network.proxy.autoconfig_url\", \"";
更正此错误:

"user_pref(\"network. -> "user_pref\(\"network.
                                   ^ 
更正此错误:

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

(在ReGEX中是一个字符字符,您应该看看它在StaskEntryString中的转移。您应该用‘\'来替换‘.'”。“.'字符将捕获任何字符。我认为您只想捕获'.'。因为正则表达式具有非常敏感的结构,所以我认为它是好的实践可读性和可维护性明智的USI。nga,以减少字符串转义和正则表达式转义之间的混淆。(在ReGEX中是一个字符字符,您应该看看它在StaskEntryString中的转移。您应该用‘\'来替换‘.'”。“.'字符将捕获任何字符。我认为您只想捕获'.'。因为正则表达式具有非常敏感的结构,所以我认为它是好的实践可读性和可维护性明智的USI。为了减少字符串转义和正则表达式转义之间的混淆,您需要转义:
“user\u pref\(\“network.proxy.autoconfig\u url\”,
(使用两个\),否则C编译器会认为它应该处理的是转义序列,而不是正则表达式的转义。与其自己转义,不如尝试使用regex.escape(startIndex)。您需要转义:
“user\u pref\”(\“network.proxy.autoconfig\u url\”,
(使用两个\),否则C编译器会认为这是一个它应该处理的转义序列,而不是regex的转义。与其自己转义,不如尝试使用regex.escape(startIndex)。