I am working on a code that can scan multiple .docx files for a keyword and then gives the whole sentence out, till a line break.
This function works great, I get every Sentence that contains the keyword till there is a line break.
My Question:
How does my RegEx have to look like when I don't want the text till the 1st linebreak, but the text up to the 2nd line break? Maybe with the right quantifier? I didn't get it to work.
My pattern: ".*" + "keyword" + ".*"
Main.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using Xceed.Words.NET;
public class Class1
{
static void Main(string[] args)
{
String searchParam = @".*" + "thiskeyword" + ".*";
List<String> docs = new List<String>();
docs.Add(@"C:\Users\itsmemario\Desktop\project\test.docx");
for (int i = 0; i < docs.Count; i++)
{
Suche s1 = new Suche(docs[i], searchParam);
s1.SearchEngine(docs[i], searchParam);
}
}
}
Suche.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using Xceed.Words.NET;
public class Suche
{
String path;
String stringToSearchFor;
List<String> searchResult = new List<String>();
public Suche(String path, String stringToSearchFor)
{
this.path = path;
this.stringToSearchFor = stringToSearchFor;
}
public void SearchEngine(String path, String stringToSearchFor)
{
using (var document = DocX.Load(path))
{
searchResult = document.FindUniqueByPattern(stringToSearchFor, RegexOptions.IgnoreCase);
if (searchResult.Count != 0)
{
WriteList(searchResult);
}
else
{
Console.WriteLine("Text does not contain keyword!");
}
}
}
public void WriteList(List<String> list)
{
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i]);
Console.WriteLine("\n");
}
}
}
Expected output is like:
"*LINEBREAK* Theres nothing nicer than a working filter for keywords. *LINEBREAK*"