The following regular expression pattern
string regExp =@"(?<=!).+?(?=!)";
can be used to exclude the prefix and suffix.
In regular expression pattern ?<= matches prefix but excludes it, in the above code the = was missing.
But the problem is we get the words in between required words as the suffix excluded will become the prefix for the next word i.e. we get
test1, middle text, othertext
from the following example whereas I think we only require between
!!
test1, othertext
The following code can be used for that purpose
string regExp =@"!(.+?)!";
string FileData="String starts here !test1! middle text !othertext! some text";
MatchCollection mc = Regex.Matches(FileData, regExp);
if (mc.Count > 0)
{
string temp = "";
foreach (Match m in mc)
{
if (m.Groups.Count > 1)
temp += m.Groups[1].Value + "\n";
}
Console.WriteLine (temp);
}