Advertisement
stanevplamen

02.8.08.SentenceExtractRegex

Jun 13th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4.  
  5. class SentencesExtractRegex
  6. {
  7.     static void SubstringCounter(string textTemp, string targetTemp)
  8.     {
  9.         string text = textTemp; // toUpper
  10.         string target = targetTemp; // toUpper
  11.         StringBuilder sb = new StringBuilder();
  12.         string[] parts = Regex.Split(text, @"\.\n|\. ");
  13.         sb.AppendFormat(@"\b{0}\b", target);
  14.         string pattern = sb.ToString();
  15.         foreach (var sentence in parts)
  16.         {
  17.             Match match = Regex.Match(sentence, pattern);
  18.             bool sign = false;
  19.             while (match.Success)
  20.             {
  21.                 sign = true;
  22.                 match = match.NextMatch();
  23.             }
  24.             if (sign == true)
  25.             {
  26.                 Console.WriteLine(sentence);
  27.             }
  28.         }
  29.  
  30.         return;
  31.     }
  32.  
  33.     static void Main()
  34.     {
  35.         string someText = "We are living in an yellow submarine. We don't have anything else. Inside the submarine is very tight. So we are drinking all the day. We will move out of it in 5 days.";
  36.         string target = "in";
  37.         SubstringCounter(someText, target);
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement