PowerShell 正则表达式平衡组匹配html文档嵌套标签
[i=s] 本帖最后由 WHY 于 2018-1-3 20:16 编辑 [/i]今天得空,逛了逛 pstips.net,翻到 [url]http://www.pstips.net/question/7423.html[/url] 这一贴,楼主要求用 PowerShell 正则表达式平衡组匹配 html 文档中所有 dl 嵌套标签。
PowerShell 脚本如下:[code]function GetBalancedConstruct {
param([ref]$refStr);
$match = [regex]::Matches($refStr.Value, $reg); #正则匹配
$match | ForEach { #遍历匹配集合
$str = $_.Groups[0].Value; #捕获组0的值赋值给变量str
Write-Host ('第 ' + (++$global:n) + ' 组'); #回显
Write-Host $str;
GetBalancedConstruct ([ref]$str.SubString(3)) #递归
}
}
$str = [IO.File]::ReadAllText('a.html', [Text.Encoding]::Default); #打开文本、读取文本内容
$reg = '(?i)<dl>((?:(?!</?dl>)[\S\s])*|<dl>(?<Open>)|</dl>(?<-Open>))*(?(Open)(?!))</dl>'; #正则表达式模式
GetBalancedConstruct ([ref]$str) #调用函数
[Console]::ReadLine() #暂停[/code]如果感兴趣的话可以继续往下看,对脚本中正则表达式模式注释一下:[code]<dl>(?:(?!</?dl>)[\S\s])*[/code]#<dl> 后面跟着非 <dl> 或者非 </dl> 的任意字符,这样的字符重复任意次;[code]<dl>(?<Open>)[/code]#遇到 <dl> 字符,将 Open 捕获组内容压入堆栈,计数加1;[code]</dl>(?<-Open>)[/code]#遇到 </dl> 字符,将 Open 捕获组内容弹出堆栈,计数减1;[code](?(Open)(?!))[/code]#相当于条件表达式 (?(Open)(?!Expression1)|Expression2), Expression1 和 Expression2 被省略。
#如果 <dl> 和 </dl> 个数不相等,Open 捕获组计数不为0,执行条件分支 (?!),由于 Expression1 被省略,匹配总是失败,回溯进行下一轮匹配;
#如果 <dl> 和 </dl> 个数相等,Open 捕获组计数等于0,执行条件分支 Expression2,由于 Expression2 被省略,也就是不进行任何操作,代表匹配成功。 顺手贴一下正则的教程吧。有说到平衡组的。
[url]http://deerchao.net/tutorials/regex/regex.htm[/url] [b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=205786&ptid=46908]2#[/url] [i]codegay[/i] [/b]
不错哦!
我也推荐一个吧,想学正则的话可以参考下:
[url]http://www.regular-expressions.info/tutorial.html[/url]
页:
[1]