.NET regex template?

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

For some strange reason, you donโ€™t need to deal with this code (in 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("\" : \"", "=") + "&";
         }
    }
}
+3
source share
2 answers

Use RegexOptions.SingleLine, notRegexOptions.Multiline

RegexOptions.Singleline

Specifies single line mode. Changes the value of a dot (.) So that it matches each character (instead of every character except \ n).

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

+5
source

You need to use the flag Singleline. Otherwise, the period does not correspond to new lines. MultiLineused to match ^and $at the beginning / end of the line. In addition, you need to avoid curly braces:

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

Source: https://habr.com/ru/post/1743404/


All Articles