C# 我想使用Regex从这个字符串GRN/GSI/2017/7中提取7
我想使用Regex从这个字符串GRN/GSI/2017/7中提取7 我试过了,但没成功C# 我想使用Regex从这个字符串GRN/GSI/2017/7中提取7,c#,asp.net,.net,regex,C#,Asp.net,.net,Regex,我想使用Regex从这个字符串GRN/GSI/2017/7中提取7 我试过了,但没成功 `string s1 = dt2.Rows[0]["Doc_No"].ToString(); string i1; string[] numbers = Regex.Split(s1, @"\D+"); foreach (string value in numbers) {
`string s1 = dt2.Rows[0]["Doc_No"].ToString();
string i1;
string[] numbers = Regex.Split(s1, @"\D+");
foreach (string value in numbers)
{
if (!string.IsNullOrEmpty(value))
{
i1 = value;
txtGRNNo.Text = i1;
}
}`
你能解释一下你提取那个数字的“规则”是什么吗?拆分字符串后,您发布的代码将在所有数字之间循环。“\d+”是一个或多个数字。因此,在您的情况下,2017和7都将匹配 你总是只想要最后一个号码吗 如果是这样,那么这可能对您有用:
string s1 = dt2.Rows[0]["Doc_No"].ToString();
txtGRNNo.Text = Regex.Split(s1, @"/").Last();
您可以使用以下正则表达式模式: 在/之间具有固定数量的字符:
[A-Z]{0,3}\/[A-Z]{0,3}\/[0-9]{0,4}\/([0-9]*)\b
动态字符数介于/:
[A-Z]*\/[A-Z]*\/[0-9]*\/([0-9]*)\b
关于如何使用正则表达式,请查看例如和