The
Regex
class can be used to retrieve the required
sub string
from the above text.
If
only first
occurrence of the sub string is required then the
Match
method of
Regex
can be used as follows:
string text = @"there is a new care here, nothing else";
Match match = Regex.Match(text,@"new.*?nothing",
RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
if (match.Success)
Console.WriteLine (match.Value);
If
multiple occurrences
of the sub string is required then the
Matches
method of
Regex
class can be used as follows:
string text2 = @"there is a new care here, nothing second new care here second time, nothing else";
MatchCollection matches = Regex.Matches(text2,@"new.*?nothing",
RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
foreach (Match m in matches)
{
Console.WriteLine (m.Value);
}