Reputation: 71
how can I extract number between two strings one is fixed and other is any thing EX. HimyDear139friend mydear111sayhi1234 imissdear121212 dear123likeorange
i nead to extract the number which is always after dear
Upvotes: 0
Views: 315
Reputation: 700192
As you will get about 92873928734 regular expression solutions that all look the same, here is a solution that doesn't use a regular expression:
// get start position
int index = str.IndexOf("dear") + 4;
// get digits
string digits = new String(str.Skip(index).TakeWhile(Char.IsDigit).ToArray());
Upvotes: 0
Reputation: 597
Answer from Aliostad is the correct one, just add the RegexOptions.IgnoreCase
option to it if you want to catch the Dear
and dear
indifferently
string expression = @"dear(\d+)";
string myString = "HimyDear139friend mydear111sayhi1234 imissdear121212 dear123likeorange";
MatchCollection matches = Regex.Matches(myString, expression);
foreach(Match m in matches)
Console.WriteLine(m.Groups[1].Value);
Console.WriteLine("Ignoring Case Option Enabled");
matches = Regex.Matches(myString, expression, RegexOptions.IgnoreCase);
foreach (Match m in matches)
Console.WriteLine(m.Groups[1].Value);
Hope that helped ;)
Upvotes: 1
Reputation: 81660
Use this expression:
string expression = "dear(\d+)";
string myString = "HimyDear139friend mydear111sayhi1234 imissdear121212 dear123likeorange"
MatchCollection matches = Regex.Matches(myString, expression);
foreach(Match m in macthes)
Console.WriteLine(m.Groups[1].Value)
Upvotes: 6