Advertisement
n4wn4w

HOMEWORK - REGex

May 17th, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.84 KB | None | 0 0
  1. 1 zadacha  ///////////////////////////////////////////////////////
  2.  
  3. string text = Console.ReadLine();
  4.         string pattern = @"(<a href=)(.*(?=>))(>)(.*(?=<))(</a>)";
  5.         string replace = @"[URL href=$2]$4[/URL]";
  6.         var replaced = Regex.Replace(text, pattern, replace);
  7.         Console.WriteLine(replaced);
  8.  
  9.  
  10. 2 zadacha  ///////////////////////////////////////////////////////
  11.  
  12.  
  13.  string text = Console.ReadLine();
  14.         string pattern = @"(<a href=)(.*(?=>))(>)(.*(?=<))(</a>)";
  15.         string replace = @"[URL href=$2]$4[/URL]";
  16.         var replaced = Regex.Replace(text, pattern, replace);
  17.         Console.WriteLine(replaced);
  18.    
  19.  
  20. 3 zadacha  ///////////////////////////////////////////////////////
  21.  
  22.  string text = Console.ReadLine();
  23.         string patternMeil = @"([a-zA-Z0-9_\-][a-zA-Z0-9_\-\.]{0,49})" +
  24.                 @"@(([a-zA-Z0-9][a-zA-Z0-9\-]{0,49}\.)+[a-zA-Z]{2,4})";
  25.         Regex regexMail = new Regex(patternMeil);
  26.         MatchCollection matches = regexMail.Matches(text);
  27.        // Console.WriteLine("Found {0} matches", matches.Count);
  28.         foreach (Match name in matches)
  29.         {
  30.             Console.WriteLine(name.Groups[0]);
  31.         }
  32.  
  33.  
  34. 4 zadacha  ///////////////////////////////////////////////////////
  35.  
  36. string word = Console.ReadLine();
  37.         string text = Console.ReadLine();
  38.         string pattern = string.Format(@"(?<=\s|^)(.*?\b{0}\b.*?(?=\!|\.|\?)[?.!])", word);
  39.         Regex regexSentence = new Regex(pattern);
  40.         MatchCollection matches = regexSentence.Matches(text);
  41.         Console.WriteLine("Found {0} matches", matches.Count);
  42.         foreach (Match sentence in matches)
  43.         {
  44.             Console.WriteLine(sentence.Groups[0]);
  45.         }
  46.  
  47.  
  48. 5 zadacha domashno  ot java basiv 21 septemvri user names
  49.  
  50. using System;
  51. using System.Collections.Generic;
  52. using System.Linq;
  53. using System.Text;
  54. using System.Text.RegularExpressions;
  55.  
  56. namespace ConsoleApplication1
  57. {
  58.     class Program
  59.     {
  60.         static void Main(string[] args)
  61.         {
  62.  
  63.             string text = Console.ReadLine();
  64.             string pattern = @"\b[a-zA-Z]\w{2,24}\b";
  65.             Regex users = new Regex(pattern);
  66.             MatchCollection matches = users.Matches(text);
  67.             List<string> valid = new List<string>();
  68.            
  69.             int first = 0;
  70.             int second = 1;
  71.             int bestSum = int.MinValue;
  72.             int sum = 0;
  73.             int biggestSum = 0;
  74.             int pos = 0;
  75.             for (int i = 0; i < matches.Count - 1; i++)
  76.             {
  77.                 int currSum = matches[i].Length + matches[i + 1].Length;
  78.                 if (currSum > biggestSum)
  79.                 {
  80.                     biggestSum = currSum;
  81.                     pos = i;
  82.                 }
  83.             }
  84.             Console.WriteLine(matches[pos]);
  85.             Console.WriteLine(matches[pos + 1]);
  86.  
  87.  
  88.  
  89.         }
  90.     }
  91. }
  92.  
  93.  
  94.  
  95. moq zadacha za 2 nai golemi stringa kato stoinost /////////////////////////////////////////////
  96.  
  97. using System;
  98. using System.Collections.Generic;
  99. using System.Linq;
  100. using System.Text;
  101. using System.Text.RegularExpressions;
  102.  
  103. namespace ConsoleApplication1
  104. {
  105.     class Program
  106.     {
  107.         static void Main(string[] args)
  108.         {
  109.             string text = Console.ReadLine();
  110.             string pattern = @"\b[a-zA-Z]\w{2,24}\b";
  111.             Regex users = new Regex(pattern);
  112.             MatchCollection matches = users.Matches(text);
  113.  
  114.             List<string> values = new List<string>();
  115.             List<int> keys = new List<int>();
  116.  
  117.  
  118.             foreach (var match in matches)
  119.             {
  120.                 if (char.IsLetter(match.ToString().First()))
  121.                 {
  122.                    values.Add(match.ToString());
  123.                 }
  124.  
  125.                
  126.             }
  127.            
  128.             int sum = 0;
  129.             string hui = "";
  130.             for (int i = 0; i < matches.Count; i++)
  131.             {
  132.                 sum = 0;
  133.                 hui = Convert.ToString(matches[i]);
  134.                 for (int j = 0; j < hui.Length; j++)
  135.                 {
  136.                    
  137.                     char charek = hui[j];
  138.                     sum += charek;
  139.                 }
  140.                 keys.Add(sum);
  141.             }
  142.  
  143.             var dict = keys.Zip(values, (k, v) => new { Key = k, Value = v })
  144.                      .ToDictionary(x => x.Key, x => x.Value);
  145.  
  146.  
  147.             List<KeyValuePair<int, string>> myList = dict.ToList();
  148.  
  149.             myList.Sort(
  150.                 delegate(KeyValuePair<int, string> firstPair,
  151.                 KeyValuePair<int, string> nextPair)
  152.                 {
  153.                     return firstPair.Key.CompareTo(nextPair.Key);
  154.                 }
  155.             );
  156.  
  157.             myList.Reverse();
  158.  
  159.             var kkk = 0;
  160.             foreach (var dr in myList)
  161.             {
  162.                 kkk++;
  163.                 if (kkk >= 11) break;
  164.                 Console.WriteLine("{0,10} = {1,3}",dr.Value, dr.Key);
  165.             }
  166.  
  167.         }
  168.     }
  169. }
  170.  
  171.  
  172. 6 zadacha  //////////////////////////////////////////////////////
  173.  
  174. using System;
  175. using System.Text;
  176. using System.Text.RegularExpressions;
  177.  
  178. class ExtractHyperlinks
  179. {
  180.     static void Main(string[] args)
  181.     {
  182.         string inputLine;
  183.         StringBuilder sb = new StringBuilder();
  184.         while (!((inputLine = Console.ReadLine()) == "END"))
  185.         {
  186.             sb.Append(inputLine);
  187.         }
  188.         string text = sb.ToString();
  189.         //     Nakov   @"<\s*a\s[^>]*?\bhref\s*=\s*('(?<url>[^']*)'|""(?<url>[^""]*)""|(?<url>\S*))[^>]*>(?<linktext>(.|\s)*?)<\s*/a\s*>";
  190.         //     Innos   @"(?<=<a\s+(?:[^>]+\s+)?href\s*=\s*)(?:""([^""]*)""|'([^']*)'|([^\s>]+))(?=[^>]*>)";
  191.         string pattern = @"<a\s+(?:[^>]+\s+)?href\s*=\s*(?:'(?<url>[^']*)'|""(?<url>[^""]*)""|(?<url>[^\s>]+))[^>]*>";
  192.         Regex users = new Regex(pattern);
  193.         MatchCollection matches = users.Matches(text);
  194.         //Console.WriteLine("Found {0} matches", matches.Count);
  195.         foreach (Match hyperlink in matches)
  196.         {
  197.             Console.WriteLine(hyperlink.Groups["url"]);
  198.         }
  199.     }
  200. }
  201.  
  202. 7 zadacha ////////////////////////////////////////////////////////////////////////////
  203.  
  204.  
  205. using System;
  206. using System.Collections.Generic;
  207. using System.Text.RegularExpressions;
  208.  
  209. class QueryMess
  210. {
  211.     static void Main(string[] args)
  212.     {
  213.         string text;
  214.         string patternRepl = @"\s{2,}";
  215.         Regex regexRepl = new Regex(patternRepl);
  216.         string pattern = @"([^=&]+)=([^&=]+)";
  217.         Regex regex = new Regex(pattern);
  218.         MatchCollection matches;
  219.         var query = new Dictionary<string, List<string>>();
  220.         while (!((text = Console.ReadLine()) == "END"))
  221.         {
  222.             text = text.Replace("%20", " ").Replace("+", " ").Replace("?", "&");
  223.             text = regexRepl.Replace(text, " ");
  224.             matches = regex.Matches(text);
  225.             foreach (Match match in matches)
  226.             {
  227.                 string field = match.Groups[1].ToString().Trim();
  228.                 string value = match.Groups[2].ToString().Trim();
  229.                 if (!query.ContainsKey(field))
  230.                 {
  231.                     query.Add(field, new List<string>());
  232.                 }
  233.                 query[field].Add(value);
  234.             }
  235.             foreach (string field in query.Keys)
  236.             {
  237.                 Console.Write("{0}=[{1}]", field, string.Join(", ", query[field]));
  238.             }
  239.             Console.WriteLine();
  240.             query.Clear();
  241.         }
  242.     }
  243. }
  244.  
  245.  
  246. 8 zadacha  /////////////////////////////////////////////////////////////////////////////////////////////////////
  247.  
  248.  
  249.  
  250. using System;
  251. using System.Collections.Generic;
  252. using System.Linq;
  253. using System.Text;
  254. using System.Text.RegularExpressions;
  255. using System.Threading.Tasks;
  256.  
  257. namespace _08.UseYourChainsBuddy
  258. {
  259.     class UseYourChainsBuddy
  260.     {
  261.         static void Main(string[] args)
  262.         {
  263.             string input = Console.ReadLine();
  264.             Regex rgx = new Regex(@"<p>(.*?)<\/p>");
  265.             Match match = rgx.Match(input);
  266.             StringBuilder sb = new StringBuilder();
  267.  
  268.             while (match != Match.Empty)
  269.             {
  270.                 string text = Regex.Replace(match.Groups[1].Value, @"[^a-z0-9]+", " ");
  271.                 text = Regex.Replace(text, @"\s+", " ");
  272.                
  273.  
  274.                 for (int i = 0; i < text.Length; i++)
  275.                 {
  276.                     if (text[i] >= 97 && text[i] <= 109)
  277.                     {
  278.                         sb.Append((char)(text[i] + 13));
  279.                     }
  280.                     else if (text[i] >= 110 && text[i] <= 122)
  281.                     {
  282.                         sb.Append((char)(text[i] - 13));
  283.                     }
  284.                     else
  285.                     {
  286.                         sb.Append(text[i]);
  287.                     }
  288.                 }
  289.                 match = match.NextMatch();
  290.             }
  291.  
  292.             Console.WriteLine(sb.ToString());
  293.  
  294.         }
  295.     }
  296. }
  297.  
  298.        
  299.  
  300. 9 zadacha   ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  301.  
  302.  
  303. using System;
  304. using System.Collections.Generic;
  305. using System.Linq;
  306. using System.Text;
  307. using System.Text.RegularExpressions;
  308. using System.Threading.Tasks;
  309.  
  310. namespace _09.SemanticHTML
  311. {
  312.     class SemanticHTML
  313.     {
  314.         static void Main(string[] args)
  315.         {
  316.             string input = Console.ReadLine();
  317.             Regex rgxOpen = new Regex("(\\s*)<(div).*((id|class)\\s*=\\s*\"(?<hoi>.*?)\").*(?=>)");
  318.             Regex rgxClose = new Regex("(\\s*)<\\/div>\\s.*?(?<kur>\\w+)\\s*-->");
  319.             List<string> result = new List<string>();
  320.             string semantic;
  321.             Match match;
  322.  
  323.             while (input != "END")
  324.             {
  325.                 if (input.Contains("<div"))
  326.                 {
  327.                     match = rgxOpen.Match(input);
  328.  
  329.                     semantic = match.Value;
  330.                     semantic = semantic.Replace(match.Groups[2].Value, match.Groups["hoi"].Value);
  331.                     semantic = semantic.Replace(match.Groups[3].Value, " ");
  332.                     semantic = Regex.Replace(semantic.Trim(), "\\s+", " ");
  333.                     semantic = match.Groups[1].Value + semantic + ">";
  334.                     result.Add(semantic);
  335.                 }
  336.                 else if (input.Contains("</div"))
  337.                 {
  338.                     match = rgxClose.Match(input);
  339.                     semantic = match.Groups[1].Value + "</" + match.Groups["kur"].Value + ">";
  340.                     result.Add(semantic);
  341.                 }
  342.                 else
  343.                 {
  344.                     result.Add(input);
  345.                 }
  346.                 input = Console.ReadLine();
  347.             }
  348.             foreach (string s in result)
  349.             {
  350.                 Console.WriteLine(s);
  351.             }
  352.         }
  353.     }
  354. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement