The below code is performing following functionality which I intend to integrate into larger application.
- Splitting large input string
inputby dot (.) character wherever it
occurs in input string. - Storing the splitted substrings into array
result[]; - In the foreach loop , a substring is matched for occurrence of
keyword. -
If match occurs , starting from position of this matched substring in original input string , upto 300 characters are to be printed.
string[] result = input.Split('.'); foreach (string str in result) { //Console.WriteLine(str); Match m = Regex.Match(str, keyword); if (m.Success) { int start = input.IndexOf(str); if ((input.Length - start) < 300) { Console.WriteLine(input.Substring(start, input.Length - start)); break; } else { Console.WriteLine(input.Substring(start, 300)); break; } }
The input is in fact large amount of text and I think this should be done by regular expression. Being a novice ,I am not able to put everything together using a regular expressions .
Match keyword. Match m = Regex.Match(str, keyword);
300 characters starting from dot (.) i.e starting from matched sentence , print 300 characters "^.\w{0,300}"
What I intend to do is :
-
Search for keyword in input text.
-
Just as a match is found , start from the sentence containing the
keyword and print upto 300 characters from input string.How should I proceed ? Please help .
If I got it right, all you need to do is find your
keywordand capture all that follows until you find first dot or reach maximum number of characters:C# code: