Skip to main content
 首页 » 编程设计

c#之正则表达式提取数字后跟特定单词

2024年01月29日23傻小

使用 Regex,我想提取后跟特定单词的数字。

位数不是有限的。

示例输入:

My address is 1234@abc.com and you can send SMS to me. 

预期结果。

1234 

在本例中,具体的词是@abc.com,需要提取这个词后面的数字。

请您参考如下方法:

使用正则表达式组:on MSDN .

在 C# 中,试试这个:

string pattern = @"(\d+)@abc\.com"; 
string input = "My address is 15464684@abc.com and you can send SMS to me"; 
Match match = Regex.Match(input, pattern); 
 
// Get the first named group. 
Group group1 = match.Groups[1]; 
Console.WriteLine("Group 1 value: {0}", group1.Success ? group1.Value : "Empty");