I have a string extension that was defined exactly like this:
public static string GetStringBetween(this string value, string start, string end)
{
start = Regex.Escape(start);
end = Regex.Escape(end);
GroupCollection matches = Regex.Match(value, start + @"([^)]*)" + end).Groups;
return matches[1].Value;
}
But when I call this:
string str = "The pre-inspection image A. Valderama (1).jpg of client Valderama is not...";
Console.WriteLine(str.GetStringBetween("pre-inspection image ", " of client"));
It doesn’t write anything. But when the str value is like this:
string str = "The pre-inspection image A. Valderama.jpg of client Valderama is not...";
It works fine. Why was it like this?
My code is in C#, framework 4, build in VS2010 Pro.
Please help. Thanks in advance.
Because you specify to exclude the character
)in the capturing group of your regex:[^)]in@"([^)]*)"And since
)appears in the first string:Valderama (1).jpg, it will not be able to match.You probably want
@"(.*)"instead.