Advertisement
dimipan80

Sentence Extractor

May 12th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.80 KB | None | 0 0
  1. /* Write a program that reads a keyword and some text from the console and prints all sentences from the text, containing that word. A sentence is any sequence of words ending with '.', '!' or '?'. */
  2.  
  3. namespace _04.SentenceExtractor
  4. {
  5.     using System;
  6.     using System.Text.RegularExpressions;
  7.  
  8.     class SentenceExtractor
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             string keyword = Console.ReadLine();
  13.             string text = Console.ReadLine();
  14.             string pattern = @"\b[A-Z][^.?!]*?\b" + keyword + @"\b.*?[!.?]";
  15.             MatchCollection matches = Regex.Matches(text, pattern);
  16.             Console.WriteLine("Output:");
  17.             foreach (Match match in matches)
  18.             {
  19.                 Console.WriteLine(match);
  20.             }
  21.         }
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement