C# 错误,c控制台应用程序中的正则表达式无效
我只想选择数字值…但它也可以获取字母表C# 错误,c控制台应用程序中的正则表达式无效,c#,regex,C#,Regex,我只想选择数字值…但它也可以获取字母表 我只想从HTML中选择数值,并使用[0-9]*作为正则表达式,但它不起作用,也不显示字母。我只想要数值,什么是正确的正则表达式?有什么想法吗?给你,你要找的是: 你能出示密码吗?示例输入和错误结果是什么?我发布了我的代码,并使用[0-9]*仅选择数值,但Regex r1=new Regex^[0-9]+$;试试这个普利斯普雷姆,普通的649卢比,比萨屋。。。。。。我只想选择649而不是常规的Rs,649..当我试图使用您的正则表达式调试代码时,我的项目不起
我只想从HTML中选择数值,并使用[0-9]*作为正则表达式,但它不起作用,也不显示字母。我只想要数值,什么是正确的正则表达式?有什么想法吗?给你,你要找的是:
你能出示密码吗?示例输入和错误结果是什么?我发布了我的代码,并使用[0-9]*仅选择数值,但Regex r1=new Regex
^[0-9]+$;试试这个普利斯普雷姆,普通的649卢比,比萨屋。。。。。。我只想选择649而不是常规的Rs,649..当我试图使用您的正则表达式调试代码时,我的项目不起作用,当我使用正则表达式时,它会显示输出。。。不知道为什么…@DamithThank@JoeClacks,这很有效。。但是在price应该为null或空的两行中,它打印1,'foreach Match ml in mclPrice{string price=ml.Groups[1]。ToString;if price!=&&price!=0{menuPriceList.Addprice;}}'您对此有什么想法吗??
private static void PizzaHutPizzaScrapper()
{
try
{
MatchCollection mclName;
MatchCollection mclPrice;
WebClient webClient = new WebClient();
string strUrl = "http://www.pizzahut.com.pk/pizzas.html";
byte[] reqHTML;
reqHTML = webClient.DownloadData(strUrl);
UTF8Encoding objUTF8 = new UTF8Encoding();
string pageContent = objUTF8.GetString(reqHTML);
Regex r = new Regex("(<p class=\"MenuDescHead\">[A-Za-z\\s*]+[0-9]*)");
// Regex r1 = new Regex("(<p class=\"MenuDescPrice\">[A-Za-z.\\s?]+[0-9]*[A-Za-z\\s?]*[0-9]*[A-Za-z.\\s?]*)");
Regex r1 = new Regex("(<p class=\"MenuDescPrice\">[0-9]*)");
mclName = r.Matches(pageContent);
mclPrice = r1.Matches(pageContent);
StringBuilder strBuilder = new StringBuilder();
string name = "";
string price = "";
List<string> menuPriceList = new List<string>();
foreach (Match ml in mclPrice)
{
price = ml.Value.Remove(0, ml.Value.IndexOf(">") + 1).Trim();
if (price != "")
{
menuPriceList.Add(ml.Value);
}
}
int j = 0;
for (int i = 0; i < mclName.Count; i++)
{
name = mclName[i].Value.Remove(0, mclName[i].Value.IndexOf(">") + 1);
if (i == 0 || i == 4)
{
price = menuPriceList[j].Remove(0, menuPriceList[j].IndexOf(">") + 1);
strBuilder.Append(name.Trim() + ", " + price.Trim() + " , PizzaHut\r\n");
j++;
}
price = menuPriceList[j].Remove(0, menuPriceList[j].IndexOf(">") + 1);
strBuilder.Append(name.Trim() + ", " + price.Trim() + " ,PizzaHut\r\n");
j++;
}`
MatchCollection mclName;
MatchCollection mclPrice;
WebClient webClient = new WebClient();
string strUrl = "http://www.pizzahut.com.pk/pizzas.html";
byte[] reqHTML;
reqHTML = webClient.DownloadData(strUrl);
UTF8Encoding objUTF8 = new UTF8Encoding();
string pageContent = objUTF8.GetString(reqHTML);
Regex nameRegex = new Regex("<p class=\"MenuDescHead\">([A-Za-z\\s]+[0-9]*)");
Regex priceRegex = new Regex("<p class=\"MenuDescPrice\">[^0-9]*([0-9]*)");
mclName = nameRegex.Matches(pageContent);
mclPrice = priceRegex.Matches(pageContent);
StringBuilder strBuilder = new StringBuilder();
List<string> menuPriceList = new List<string>();
foreach (Match ml in mclPrice)
{
string price = ml.Groups[1].ToString();
if (price != "" && price != "0")
{
menuPriceList.Add(price);
}
}
int j = 0;
for (int i = 0; i < mclName.Count; i++)
{
string price;
string name = mclName[i].Groups[1].ToString();
if (i == 0 || i == 4)
{
price = menuPriceList[j];
strBuilder.Append(name.Trim() + ", " + price.Trim() + " , PizzaHut\r\n");
j++;
}
price = menuPriceList[j];
strBuilder.Append(name.Trim() + ", " + price.Trim() + " ,PizzaHut\r\n");
j++;
}
Console.WriteLine(strBuilder.ToString());