Advertisement
Guest User

pr 13 № 9

a guest
Oct 21st, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.47 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.  
  7. namespace ForDZ
  8. {
  9.     class pr_13_9
  10.     {
  11.         public static void Task()
  12.         {
  13.             string[] input = {
  14.                 "Hello, I am a string.",
  15.                 "This program is free-software; you can copy it.",
  16.                 "This program is    free-software; you can copy it.",
  17.                 "This program is Free-software; you can copy it.",
  18.                 "In his house at Rl-yeh dead Cthulhu waits dreaming.",
  19.                 "Ph-nglui mglw-mafh Cthulhu R-lyeh wgah-nagl fhtagn.",
  20.                  "Is this string work.",
  21.                  "First test text. Is it work.",
  22.             };
  23.  
  24.             string abbreviation = "([A-Z]+)";
  25.             string first_latter_big = "(([A-Z])([a-z]*))";
  26.             string first_latter_low = "([a-z]+)"; ;
  27.             string word_first_part = "(" + first_latter_big + "|" + first_latter_low + ")";
  28.             string word_second_part = "((-" + first_latter_low + ")?)";
  29.             string lower_word = "(" + word_first_part + word_second_part + ")";
  30.             string first_lower_word = "(" + first_latter_big + word_second_part + ")";
  31.             string word = "(" + abbreviation + "|" + lower_word + ")";
  32.             string first_word = "(" + abbreviation + "|" + first_lower_word + ")";
  33.             string separators = @"((( - ){1})|((, ){1})|((; ){1})|((, - ){1})|(( ){1}))"; //"((;)|(((,)?)(( ){1})((- )?)))";
  34.             string final = @"((...)|(\?)|(!)|(.))";
  35.             string sentence = "(" + first_word + "(" + separators + word + ")*" + final + ")";
  36.  
  37.             string expression = "^(" + sentence + "(( )" + sentence + ")*)$";
  38.             Regex regex = new Regex(expression);
  39.  
  40.             test(regex, input);
  41.         }
  42.  
  43.         static void test(Regex regex, string[] inp)
  44.         {
  45.             foreach (string s in inp)
  46.             {
  47.                 string t = regex.Match(s).Value;
  48.                 //Console.WriteLine($"> {s} ({s.Length}) -> {t} ({t.Length}) : {s.Length == t.Length}");
  49.                 Console.WriteLine($"> {s} -> {(s.Length == t.Length ? "YES" : "NO")}");
  50.             }
  51.             Console.WriteLine();
  52.             Console.WriteLine();
  53.         }
  54.     }
  55.  
  56.     class Program
  57.     {
  58.         static void Main(string[] args)
  59.         {
  60.             pr_13_9.Task();
  61.  
  62.             Console.WriteLine("\n\nend.");
  63.             Console.ReadKey();
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement