我现在有一段HTML源代码:其中有一些HMTL如下:1:<li title=" 209139">货号: 209139</li> 红色是动态的;就是可以不同的;要写一个正则表达式取到:2091392:string str = @"<strong id=""J_StrPrice"" >1499.00 - 1999.00</strong>string str = @"<strong id=""J_StrPrice"" >2000</strong>上面这两个的;要是第一个就把它分别取出两个数的!要是是第二种就只取出一个值的;这个正则要怎么写啊!就是判断并取出值的;??????(数据库有两个字段)第一个取到的值为:1499 跟 1999
第二个取到的值为:2000
第二个取到的值为:2000
解决方案 »
- NB----NB大牛------请进-----下载大小问题
- 网上下载的ASP源代码修改了一下,运行出现的问题,在编译向该请求提供服务所需资源的过程中出现错误
- ASP.NET AJAX异步调用Web Service的问题
- 输入框记忆错位,这种问题是什么原因,怎么解决啊
- 在.NET 2.0 中使用类库中的Mail类发信,实际是用 CDOSYS组件来发的,它只一个封状???
- 创建图形是的问题!!
- 有关空间、域名、ip地址以及如何指向的问题
- 可以刷新showModalDialog吗?
- .net前台视频播放
- 请问怎么把字符串变成对象名???
- json菜鸟:ashx如何接受前端传来的json呢?
- 在ASP .NET, xmlhttprequest.send()中第二个URL相对网址怎么写?在线等
2. (?i)<strong\sid=(['"]?)J_StrPrice\1[^>]*>([^-<>]+)(-[^-<>]+)?</strong>
取分组2 和 3
(?<=[\D])\d*?(?=[.])
(?<=>)\d*?(?=<)
要取到红色的字;要怎么写
void Main()
{
string str1=@"<li title="" 209139"">货号: 209139</li>";
Console.WriteLine(Regex.Match(str1,@"(?i)(?<=<li[^>]*>货号\D+)\d+(?=</li>)").Value);
List<string> list=new List<string>{@"<strong id=""J_StrPrice"" >1499.00 - 1999.00</strong>",@"<strong id=""J_StrPrice"" >2000</strong>"};
Regex reg=new Regex(@"(?i)<strong\sid=(['""]?)J_StrPrice\1[^>]*>(\d+)[0\-\s\.]*(\d+)?[^>]*</strong>");
foreach(string s in list)
{
Match m =reg.Match(s);
Console.WriteLine(Convert.ToInt32(m.Groups[2].Value.Trim()));
if(!string.IsNullOrEmpty(m.Groups[3].Value)) Console.WriteLine(Convert.ToInt32(m.Groups[3].Value));
}
/*
209139
1499
1999
2000
*/
}
string str = ":<li title=\" 209139\">货号: 209139</li>";
Regex reg = new Regex(@"(?<=<li[^>]*?>(?:(?!</?li).)*)\d+");
Response.Write(reg.Match(str).Value + "<br/>"); List<string> list = new List<string>() { @"<strong id=""J_StrPrice"" >1499.00 - 1999.00</strong>", @"<strong id=""J_StrPrice"" >2000</strong>" };
reg = new Regex(@"(?<=<strong\s*id=""J_StrPrice""\s*>(?:(?!</?strong).)*)((?<Value>\d+(\.\d+)?)[^\d]*)*");
foreach (string s in list)
{
Match m = reg.Match(s);
foreach (Capture c in m.Groups["Value"].Captures)
Response.Write(c.Value + "<br/>");
}
/*
209139
1499.00
1999.00
2000
*/