求正则表达式(huangwenquan123)

wx8849 2012-04-13 04:23:40
获取到的html代码有多个select标签 我想获取
<select name="ProductsSearchLayout$BottomSectionPager$PagerLinksDropDownList" id="ProductsSearchLayout_BottomSectionPager_PagerLinksDropDownList" class="paginateDD" onChange="MM_jumpMenu('parent',this,0)">
<option selected="selected" value="/abella.aspx?avs=Brand%7cAbella&fa=1">Page 1</option>

</select>

这个里面的option value 和Page 1怎么获取呢
...全文
97 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangwenquan123 2012-04-13
  • 打赏
  • 举报
回复
写了下。

string str= @"<select name=""ProductsSearchLayout$BottomSectionPager$PagerLinksDropDownList"" id=""ProductsSearchLayout_BottomSectionPager_PagerLinksDropDownList"" class=""paginateDD"" onChange=""MM_jumpMenu('parent',this,0)"">
<option selected=""selected"" value=""/abella.aspx?avs=Brand%7cAbella&fa=1"">Page 1</option>
<option selected=""selected"" value=""/abella.aspx?avs=Brand%7cAbella&fa=2"">Page 2</option>
<option selected=""selected"" value=""/abella.aspx?avs=Brand%7cAbella&fa=3"">Page 3</option>

</select>";
Regex reg = new Regex(@"(?is)(?<=<select[^>]*?class=""paginateDD""[^>]*?>(?:(?!</?select).)*)<option[^>]*?value=(['""\s]?)([^'""]+)\1[^>]*?>(.*?)</option>");
foreach (Match m in reg.Matches(str))
Console.WriteLine(m.Groups[2].Value + " " + m.Groups[3].Value);
/*
/abella.aspx?avs=Brand%7cAbella&fa=1 Page 1
/abella.aspx?avs=Brand%7cAbella&fa=2 Page 2
/abella.aspx?avs=Brand%7cAbella&fa=3 Page 3

*/

huangwenquan123 2012-04-13
  • 打赏
  • 举报
回复
回家在写下。还在公司处理事情。
wx8849 2012-04-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
引用 2 楼 的回复:

引用 1 楼 的回复:
C# code


static void Main(string[] args)
{

string str = @"<select name=""ProductsSearchLayout$BottomSectionPager$PagerLinksDropDownList"" id=""ProductsSearchLayout_……
[/Quote]

多个option 获取不到呢?
wx8849 2012-04-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
引用 2 楼 的回复:

引用 1 楼 的回复:
C# code


static void Main(string[] args)
{

string str = @"<select name=""ProductsSearchLayout$BottomSectionPager$PagerLinksDropDownList"" id=""ProductsSearchLayout_……
[/Quote]

因为会有不同的页面,他的ID可能随时会变,他的class 肯定是只有分页这个,所以只能只要
porschev 2012-04-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

引用 1 楼 的回复:
C# code


static void Main(string[] args)
{

string str = @"<select name=""ProductsSearchLayout$BottomSectionPager$PagerLinksDropDownList"" id=""ProductsSearchLayout_……


我只想根据……
[/Quote]

可以。。。

Regex re = new Regex(@"(?is)<select[^>]+id=""ProductsSearchLayout_BottomSectionPager_PagerLinksDropDownList""[^>]+>\s*<option[^>]+value=""([^""]+)"">([^<]+)</option>", RegexOptions.None);

------------------->

Regex re = new Regex(@"(?is)<select[^>]+class=""paginateDD""[^>]+>\s*<option[^>]+value=""([^""]+)"">([^<]+)</option>
", RegexOptions.None);



建议:你不是说有很多Select标签吗?你如果想取你给出来的Select标签,其实用ID最好,ID才是唯一的,class样式名不是,当然你这个页面中就只有一个class为paginateDD的SELECT标签

  • 打赏
  • 举报
回复
value="/,page 1"
spilt[","]
wx8849 2012-04-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
C# code


static void Main(string[] args)
{

string str = @"<select name=""ProductsSearchLayout$BottomSectionPager$PagerLinksDropDownList"" id=""ProductsSearchLayout_……
[/Quote]

我只想根据class来拿 能不能根据什么name和ID来?
porschev 2012-04-13
  • 打赏
  • 举报
回复


static void Main(string[] args)
{

string str = @"<select name=""ProductsSearchLayout$BottomSectionPager$PagerLinksDropDownList"" id=""ProductsSearchLayout_BottomSectionPager_PagerLinksDropDownList"" class=""paginateDD"" onChange=""MM_jumpMenu('parent',this,0)"">
<option selected=""selected"" value=""/abella.aspx?avs=Brand%7cAbella&fa=1"">Page 1</option>

</select>";

Regex re = new Regex(@"(?is)<select[^>]+id=""ProductsSearchLayout_BottomSectionPager_PagerLinksDropDownList""[^>]+>\s*<option[^>]+value=""([^""]+)"">([^<]+)</option>", RegexOptions.None);

Match ma = re.Match(str);
Console.WriteLine(ma.Groups[1].Value); // 输出值为:/abella.aspx?avs=Brand%7cAbella&fa=1
Console.WriteLine(ma.Groups[2].Value); // 输出值为:Page 1
Console.ReadLine();
}

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧