I am trying to construct a regex patter that enables me to check if a specific combination of words appears within a sentence.
Text Example
In the body of your question, start by expanding on the summary you put in the title. Explain how you encountered the problem you're trying to solve, and any difficulties that have prevented you from solving it yourself. The first paragraph in your question is the second thing most readers will see, so make it as engaging and informative as possible.
Now i am trying to create a pattern that will tell me if any sentence in this text contains a combination of words in any order.
Example combination:
summary, question
Example code:
Regex regex = new Regex(@"(summary|question).*\w\.");
Match match = regex.Match("In the body of your question, start by expanding on the summary you put in the title. Explain how you encountered the problem you're trying to solve, and any difficulties that have prevented you from solving it yourself. The first paragraph in your question is the second thing most readers will see, so make it as engaging and informative as possible.");
if (match.Success)
{
Console.WriteLine("Success");
} else {
Console.WRiteLine("Fail");
}
Output:
Success
Example Code:
Regex regex = new Regex(@"(summary|question).*\w\.");
Match match = regex.Match("Explain how you encountered the problem you're trying to solve, and any difficulties that have prevented you from solving it yourself. The first paragraph in your question is the second thing most readers will see, so make it as engaging and informative as possible.");
if (match.Success)
{
Console.WriteLine("Success");
} else {
Console.WRiteLine("Fail");
}
Output:
Fail
My ultimate goal is to read any number of words from user (1..n), construct them into regex pattern string and use that pattern to check against any text.
e.g. (please ignore the faulty pattern i am just using visual representation)
Words: question, summary
pattern: (question|summary).*\w
Words: user, new, start
pattern: (user|new|start).*\w
I really hope this makes sense. I am relearning regex (haven't used it in over decade).
EDIT 1 (REOPEN JUSTIFICATION):
I have reviewed some answers that were done previously and am little bit closer.
My new pattern is as follows:
/^(?=.*Reference)(?=.*Cheatsheet)(?=.*Help).*[\..]/gmi
But as per example here https://regex101.com/r/m2HSVq/1 it doesn't fully work. It looks for the word combination within the whole paragraph, rather than sentence.
As per original text, I want to only return match if within sentence (delimited by full stop or end of text).
My fallback option is to split string at full stops, then do individual matches if i can't find solution.