如何提取两个字符串之间的数字一个是固定的,另一个是任意的
本文关键字:一个 任意 另一个 之间 提取 何提取 两个 字符串 数字 | 更新日期: 2023-09-27 18:26:23
如何在两个字符串之间提取数字一个是固定的,另一个是任意的前任。HimyDear139朋友我亲爱的111sayhi1234imissdear12122dear123类似橙色
我想提取总是在亲爱的之后的数字
使用以下表达式:
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)
Aliostad的答案是正确的,如果你想无差别地捕捉Dear
和dear
,只需添加RegexOptions.IgnoreCase
选项
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);
希望有所帮助;)
尝试使用以下正则表达式:
/dear('d+)/i
您可以使用类似的东西
/dear('d*)/
正如您将获得的92873928734个正则表达式解决方案一样,这里有一个不使用正则表达式的解决方案:
// get start position
int index = str.IndexOf("dear") + 4;
// get digits
string digits = new String(str.Skip(index).TakeWhile(Char.IsDigit).ToArray());