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

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

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...