The problem is this:
I want to find a regex in a textfile and get the complete block of text
Text example:
text text text text text text text text text
!
title
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
!
text text text text text text text text text
finding the “title” part is easy but I want to get the following result:
title
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
What is the best way to go? Working with a regex pattern or selecting the text until I get a “!”? (I want to have simple/fast readable code)
Code for finding a pattern: (with rtxtText as the richtextbox)
private String searchInfo(String pattern)
{
String text = rtxtText.Text;
Regex regExp = new Regex(pattern);
String result = "";
foreach (Match match in regExp.Matches(text))
{
result += "\n" + match.ToString();
}
return result;
}
Your Regex be changed to contain the unknown characters as well, like
titlethen
[^!]*([^ ]means something not in this set, so[^!]*is everything except!in any number)Regex regex = new Regex(“title[^!]*”, RegexOptions.SingleLine);
MatcheCollection matches = regex.Matches(text);