Advertisement
Guest User

18. Extract sentences by keyword

a guest
Jan 2nd, 2017
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Text.RegularExpressions;
  7.  
  8. namespace _18.Extract_sentences_by_keyword
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             var word = Console.ReadLine().ToLower();
  15.             var sentences = Console.ReadLine();
  16.             var pattern = $"[^.!?;]*(({word}\\W)|(\\W{word}\\W))[^.!?;]*";
  17.  
  18.             Regex regex = new Regex(pattern);
  19.             MatchCollection matches = Regex.Matches(sentences, pattern);
  20.  
  21.             var count = 0;
  22.             foreach (Match match in matches)
  23.             {
  24.                 if(count == 0)
  25.                 {
  26.                     Console.WriteLine(match.ToString());
  27.                 }
  28.                 else
  29.                 {
  30.                     var sentence = Convert.ToString(match);
  31.                     Console.WriteLine(sentence.Substring(1));
  32.                 }
  33.                
  34.                 count++;
  35.             }
  36.  
  37.  
  38.             }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement