2010-04-29 45 views
3
var flashvars = { 
     "client.allow.cross.domain" : "0", 
     "client.notify.cross.domain" : "1", 
}; 

由於一些奇怪的原因不是想要解析此代碼(在C#中)。.NET正則表達式模式?

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("\" : \"", "=") + "&"; 
     } 
    } 
} 
+0

@Scott:只是迂腐:C#是具有在所有(不同的JavaScript)沒有正則表達式支持的編程語言。 OTOH,.NET Framework有System.Text.RegularExpressions.Regex類。 – 2010-04-29 19:41:56

回答

2

您需要使用Singleline標誌。否則,一段時間不匹配新行。 MultiLine用於使^$在行首/行尾匹配。此外,你需要躲避大括號:

Regex flashVars = new Regex(@"var flashvars = \{(.*?)\}", RegexOptions.Singleline | RegexOptions.IgnoreCase); 
+0

我似乎無法找到DotAll標誌,你確定它是一個有效的C#.NET RegexOption? – Scott 2010-04-29 19:41:16

+0

DotAll被稱爲單行,看到這個答案:http://stackoverflow.com/questions/2740176/net-regex-pattern/2740273#2740273。 – vfilby 2010-04-29 19:42:22

+0

已修復。用於Python的命名。 .NET等價物是'Singleline'。 – 2010-04-29 19:42:45