Power Query中RegexMatch返回全匹配如何解决?RegexExtractGroup解析
Power Query正则匹配捕获组提取方案
在Power Query中使用RegexMatch函数进行正则匹配时,受工具本身的正则特性限制,有时只会返回完整匹配结果,无法直接获取目标捕获组。针对这个问题,可以使用专门的JavaScript函数RegexExtractGroup提取特定捕获组(也可基于RegexMatch改造实现相同效果)。
以下是原生RegexMatch函数的代码:
//regexmatch let regexMatch=(text as nullable text, pattern as nullable text, optional flags as nullable text) as list => let f=if flags = null or flags ="" then "" else flags, l1 = List.Transform({text, pattern}, each Text.Replace(_, "\\", "\\\\")), l2 = List.Transform(l1, each Text.Replace(_, "'", "\'")), t = Text.Format("<script>var txt='#{0}';document.write(txt.match(new RegExp('#{1}','#{2}')));</script>", List.Combine({l2,{f}})), r=Web.Page(t)[Data]{0}[Children]{0}[Children], Output=if List.Count(r)>1 then Text.SplitAny(r{1}[Text]{0},",") else {} in Output in regexMatch
RegexExtractGroup函数的核心作用就是解决上述仅返回全匹配结果的问题,可精准提取正则表达式中定义的指定捕获组内容。
内容的提问来源于stack exchange,提问作者Nick
相关产品推荐
相关产品推荐

