c# 用正则表达式得到指定的值

2025-01-25 04:34:29
推荐回答(5个)
回答1:

string a = "singfo.com/product/list63.html";
string b = a.Substring(a.IndexOf("63"),"63".Length) .ToString();

回答2:

public static int GetNum(string s)
{
Regex r = new Regex("(?\\d+)\\.html");

Match m = r.Match(s);
if (m.Success)
{
return Convert.ToInt32(m.Groups["num"].Value);
}
throw new ArgumentException("没有匹配的数字!");
}

回答3:

substring(a,a.Indexof(b),b.Length)

回答4:

使用正则方法提取:
string strHTML = @"singfo.com/product/list63453.html";
string pattern = @"list\d*\.html$";
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
string outString = "";
System.Text.RegularExpressions.MatchCollection mc = reg.Matches(strHTML);
if (mc.Count > 0)
{
foreach (System.Text.RegularExpressions.Match m in mc)
{
outString = m.Value.ToString();
}
}
outString = outString.Replace("list", "").Replace(".html", "");
MessageBox.Show(outString);

回答5:

list前面的是固定的?