C# .NET正则表达式模式?

C# .NET正则表达式模式?,c#,.net,regex,C#,.net,Regex,出于某种奇怪的原因,我不想用这段代码(用C#)进行解析 您需要使用单线标志。否则,句点与新行不匹配多行用于使^和$在行的开始/结束处匹配。此外,还需要避开花括号: private void parseVariables() { String page; Regex flashVars = new Regex("var flashvars = {(.*?)}", RegexOptions.Multiline | RegexOptions.IgnoreCase);

出于某种奇怪的原因,我不想用这段代码(用C#)进行解析


您需要使用
单线
标志。否则,句点与新行不匹配<代码>多行用于使
^
$
在行的开始/结束处匹配。此外,还需要避开花括号:

private void parseVariables() {
       String page;
       Regex flashVars = new Regex("var flashvars = {(.*?)}", RegexOptions.Multiline | RegexOptions.IgnoreCase);
       Regex var = new Regex(@"""(.*?)"",", RegexOptions.Multiline | RegexOptions.IgnoreCase);
       Match flashVarsMatch;
       MatchCollection matches;
       String vars = "";

       if (!IsLoggedIn)
       {
            throw new NotLoggedInException();
       }

       page = Request(URL_CLIENT);

       flashVarsMatch = flashVars.Match(page);

       matches = var.Matches(flashVarsMatch.Groups[1].Value);

       if (matches.Count > 0)
       {
         foreach (Match item in matches)
         {
            vars += item.Groups[1].Value.Replace("\" : \"", "=") + "&";
         }
    }
}

使用
RegexOptions.SingleLine
而不是
RegexOptions.Multiline

RegexOptions.Singleline

指定单线模式。更改点(.)的含义,使其匹配每个字符(而不是除\n之外的每个字符)


我似乎找不到DotAll标志,您确定它是C#.NET的有效RegexOption吗?DotAll被称为单线,请参见以下答案:。已修复。习惯于Python的命名。NET的等价物是
Singleline
。您必须用另一个字符串来转义c字符串的反斜杠,因此它应该是
“var flashvars=\\{(.*?\\}”
@Philip Daubmeier:将字符串设置为原始字符串。@Scott:为了学究起见:c是一种完全不支持正则表达式的编程语言(与JavaScript不同)。NET Framework的System.Text.RegularExpressions.Regex类已修复。谢谢维菲尔比和马克斯·沙瓦布基。
private void parseVariables() {
       String page;
       Regex flashVars = new Regex("var flashvars = {(.*?)}", RegexOptions.Multiline | RegexOptions.IgnoreCase);
       Regex var = new Regex(@"""(.*?)"",", RegexOptions.Multiline | RegexOptions.IgnoreCase);
       Match flashVarsMatch;
       MatchCollection matches;
       String vars = "";

       if (!IsLoggedIn)
       {
            throw new NotLoggedInException();
       }

       page = Request(URL_CLIENT);

       flashVarsMatch = flashVars.Match(page);

       matches = var.Matches(flashVarsMatch.Groups[1].Value);

       if (matches.Count > 0)
       {
         foreach (Match item in matches)
         {
            vars += item.Groups[1].Value.Replace("\" : \"", "=") + "&";
         }
    }
}
Regex flashVars = new Regex(@"var flashvars = \{(.*?)\}", RegexOptions.Singleline | RegexOptions.IgnoreCase);