c# - .NET 正则表达式模式?

标签 c# .net regex

var flashvars = {
        "client.allow.cross.domain" : "0", 
        "client.notify.cross.domain" : "1",
};

出于某些奇怪的原因,不想使用此代码(在 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

Specifies single-line mode. Changes the meaning of the dot (.) so it matches every character (instead of every character except\n).

http://msdn.microsoft.com/en-us/library/443e8hc7(vs.71).aspx

关于c# - .NET 正则表达式模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2740176/

相关文章:

c# - 我们可以将访问 token 设置为不过期吗?

c# - 如何允许 SignalR 使用 EF6 将更新从数据库推送到浏览器

c# - 如何从头开始实现异步 I/O 绑定(bind)操作?

c# - 在 App.config 中设置 WCF ClientCredentials

c# - 每个托管线程是否都有自己对应的 native 线程?

regex - 正则表达式片段 : match arbitrary number of groups and transform to CamelCase

java - 在java正则表达式中如何捕获正斜杠后跟.(点)

c# - 在 Word (VSTO) 中并排比较两个 RTF 文档

c# - Pivot 上的控件消失

java - 正则表达式匹配不跟斜杠字符的双引号