正则表达式获取嵌套HTML标签中的内容

HTML代码如下:

<div class='s-variant-selections__options '>
    <div class='form-group s-form-group s-with-stack-label'>
        <div class='s-variant-selections__value'>
            <span id='priority0'>316 不锈钢</span>
        </div>
        <label for='priority0'>阀体材料</label>
    </div>
    <div class='form-group s-form-group s-with-stack-label'>
        <div class='s-variant-selections__value'>
            <span id='priority1'>3/4 in.</span>
        </div>
        <label for='priority1'>接头 1 尺寸</label>
    </div>
    <div class='form-group s-form-group s-with-stack-label'>
        <div class='s-variant-selections__value'>
            <span id='priority2'>世伟洛克® 卡套管接头</span>
        </div>
        <label for='priority2'>接头 1 类型</label>
    </div>
    <div class='form-group s-form-group s-with-stack-label'>
        <div class='s-variant-selections__value'>
            <span id='priority3'>3/4 in.</span>
        </div>
        <label for='priority3'>接头 2 尺寸</label>
    </div>
    <div class='form-group s-form-group s-with-stack-label'>
        <div class='s-variant-selections__value'>
            <span id='priority4'>内螺纹 NPT</span>
        </div>
        <label for='priority4'>接头 2 类型</label>
    </div>
    <div class='form-group s-form-group s-with-stack-label'>
        <div class='s-variant-selections__value'>
            <span id='priority5'>3/4 in.</span>
        </div>
        <label for='priority5'>接头 3 尺寸</label>
    </div>
    <div class='form-group s-form-group s-with-stack-label'>
        <div class='s-variant-selections__value'>
            <span id='priority6'>世伟洛克® 卡套管接头</span>
        </div>
        <label for='priority6'>接头 3 类型</label>
    </div>
    <div class='form-group s-form-group s-with-stack-label'>
        <div class='s-variant-selections__value'>
            <span id='priority7'>标准清洁和包装 (SC-10)</span>
        </div>
        <label for='priority7'>清洁工艺</label>
    </div>
    <div class='form-group s-form-group s-with-stack-label'>
        <div class='s-variant-selections__value'>
            <span id='priority8'>否</span>
        </div>
        <label for='priority8'>流量限制器</label>
    </div>
    <div class='form-group s-form-group s-with-stack-label'>
        <div class='s-variant-selections__value'>
            <span id='priority9'>否</span>
        </div>
        <label for='priority9'>内孔直通</label>
    </div>
</div>

我想要获取到所有div标签class='form-group s-form-group s-with-stack-label'中的内容,并且分组显示

则:

string className = "form-group s-form-group s-with-stack-label";//div标签class值

string regString = @"<(?<HtmlTag>[\w]+)[^>]*\s[cC][lL][aA][sS][sS]=(?<Quote>[']?){0}(?(Quote)\k<Quote>)[']?[^>]*>(((?<Nested><\k<HtmlTag>[^>]*>)|</\k<HtmlTag>>(?<-Nested>)|.*?)*)</\k<HtmlTag>>";

regString = string.Format(regString, className);//方便阅读

MatchCollection m = Regex.Matches(htmlString, regString, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline);//HTML代码中的属性值我用的单引号,双引号大家再去研究

以上方式可以获取多个div中的内容,循环即可。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值