Advertisement
vlad0

Sample Exam PHP Vars

Feb 4th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace _01.PHPVars
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             StringBuilder source = new StringBuilder();
  13.             string input;
  14.             string[] pattern = {
  15.                                    @"#.*", //remove # comments
  16.                                    @"//.*", //remove // comments
  17.                                    @""".*(/\*).*(\*/).*""", // ignore /* */ comments in quotes
  18.                                    @"(\\){2}", //remove the double backslash to differ from \\$var - not escaped var
  19.                                    @"\\\$" //remove it because it is escaped var
  20.                                };
  21.             do
  22.             {
  23.                 input = (Console.ReadLine());
  24.                 foreach (string patternString in pattern)
  25.                 {
  26.                     foreach (Match match in Regex.Matches(input, patternString))
  27.                     {
  28.                         input = input.Replace(match.ToString(), " ");
  29.                     }
  30.                    
  31.                 }
  32.  
  33.                 source.Append(input);
  34.             } while (input != "?>");
  35.  
  36.             string text = source.ToString();
  37.             string patternMultiLineComment = @"/\*.*?\*/"; //remove all multiline comments
  38.             foreach (Match match in Regex.Matches(text, patternMultiLineComment))
  39.             {
  40.                 text = text.Replace(match.Value, "");
  41.             }
  42.  
  43.             Dictionary<string, int> variables = new Dictionary<string, int>();
  44.  
  45.             string patternVars = @"\$(\w{1,})"; //get var names with 1 or more symbols (a-Z0-9_)
  46.             foreach (Match match in Regex.Matches(text, patternVars))
  47.             {
  48.                 string phpVar = match.Groups[1].Value;
  49.                 if (!variables.ContainsKey(phpVar))
  50.                 {
  51.                     variables.Add(phpVar, 1);
  52.                    
  53.                 }
  54.             }
  55.  
  56.             List<string> list = variables.Keys.ToList();
  57.             list.Sort(StringComparer.Ordinal);
  58.             Console.WriteLine(list.Count);
  59.             source.Clear();
  60.             foreach (string item in list)
  61.             {
  62.                 source.Append(item);
  63.                 source.Append("\n");
  64.             }
  65.             Console.WriteLine(source);
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement