You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Power Query函数中将用户参数传入正则表达式字符串

Power Query自定义例外参数无法传入正则表达式的问题求助

我有一个用正则表达式分割文本为句子的Power Query函数,但测试发现存在匹配失效的情况——比如包含“St. Bernard”的文本会在“St.”的缩写点处被错误拆分。为此我修改了正则表达式,想加入例外规则忽略这类情况,并集成到Excel中允许用户自定义例外参数,但目前无法将用户输入的参数传入函数的正则表达式字符串里。

以下是我的实现代码,运行时出现错误,尝试过Record.FromTable{0}等方法传递参数但仍有各类错误,求可行的解决办法。


现有代码实现

主查询:Sentences From text

let
        
        Exceptions = Exceptions,
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Text", type text}}),
        #"Replaced Value1" = Table.ReplaceValue(#"Changed Type","#(lf)"," ",Replacer.ReplaceText,{"Text"}),
        #"Replaced Value" = Table.ReplaceValue(#"Replaced Value1","'","&apos",Replacer.ReplaceText,{"Text"}),
        #"Invoked Custom Function" = Table.AddColumn(#"Replaced Value", "fnRegexReplace", each fnRegexReplace([Text], "\s*((?:\b(?:[djms]rs?"&Exceptions&")\.|\b(?:[a-z]\.){2,}|\.\d[\d.]*|\.(?:com|net|org)\b|[^.?!])+(?:[.?!]+|$))", "$1|")),
        #"Removed Other Columns" = Table.SelectColumns(#"Invoked Custom Function",{"fnRegexReplace"}),
        #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Removed Other Columns", {{"fnRegexReplace", Splitter.SplitTextByDelimiter("|", QuoteStyle.None), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "fnRegexReplace"),
        #"Filtered Rows" = Table.SelectRows(#"Split Column by Delimiter", each ([fnRegexReplace] <> ""))
    in
        #"Filtered Rows"

例外参数查询:Exceptions

let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Added Custom" = Table.AddColumn(Source, "Custom", each "Exceptions"),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"Custom"}, {{"Exceptions", each Text.Combine([#"Do not split if:"], "|"), type text}})
in
    #"Grouped Rows"

自定义正则替换函数:fnRegexReplace

(x,y,z)=>
let 
   
   y = Text.Replace(y,"\\","\\\\"),
   
   Source = Web.Page(
                     "<script>var x="&"'"&x&"'"&";var z="&"'"&z&
                     "'"&";var y=new RegExp('"&y&"','gmi');
                     var b=x.replace(y,z);document.write(b);</script>")
                     [Data]{0}[Children]{0}[Children]{1}[Text]{0}
in 
   Source

错误信息

错误截图


测试文本数据

Highly Flammable Liquid Flam. H223 Liq. H334. 
St. Bernard Dog was present.
The MW of gold is 100.1. Solubility is 40mg/L.

内容的提问来源于stack exchange,提问作者Nick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 18:24:26