在 excel 中使用正则表达式从 units/string 中提取第一个 integers/decimals

Extract first integers/decimals from units/string using regex in excel

我希望使用正则表达式函数分隔以下数据,如下所示:

要使用的函数:

let   fx=(text,regex)=>
    Web.Page(
        "<script>
            var x='"&text&"';
            var y=new RegExp('"&regex&"','g');
            
            var b=x.match(y);
            document.write(b);
        </script>")[Data]{0}[Children]{0}[Children]{1}[Text]{0}

in
fx

农产品:

正则表达式 - \d+\.?\d+

这成功提取了数值,但是:

  1. 我不确定这是否是删除第一个 integer/number 的正确正则表达式。
  2. 我不确定如何使用正则表达式只提取单位。尽管进行了各种尝试,但似乎 运行 出错了。例如\D+ 不 return 非数值,尽管它在 link 上工作。据说 15 ng/m3 如果这确实有效,它只会 return ng/m3。请问是不是函数本身有问题

M代码:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    #"Invoked Custom Function1" = Table.AddColumn(#"Changed Type", "fnRegexExtr2", each fnRegexExtr2([Column1], "\d+\.?\d+")),
    #"Invoked Custom Function" = Table.AddColumn(#"Invoked Custom Function1", "fnRegexExtr2.1", each fnRegexExtr2([fnRegexExtr2], "\D+"))
in
    #"Invoked Custom Function"

更新为“^[^\s]+”:

数据:

1200 mg/kg bw/day
24 mg/kg/day
0.79 mg/kg bw/day
15 ng/m3
15 ng/m 3
Not Limited
 30mg/m³

感谢发帖。看起来您需要匹配第一个 space 的字符。为此,您可以使用以下正则表达式。

^[^\s]+

函数本身没有问题,问题在于使用的模式:


数字部分:您目前使用的\d+\.?\d+基本上是指; “任何 1 位以上的数字后跟一个可选的点和至少另外 1 位以上的数字”。因此,字符串中的任何位置至少有两位数。适当的正则表达式是:

^\d+(?:\.\d+)?

含义:

  • ^ - Start-line锚点;
  • \d+ - 1+(贪心)数字;
  • (?:\.\d+)? - 可选 non-capture 组以匹配文字点后跟至少 1+ 个数字。

单位: 您的模式 [\D+][2] 匹配第一个 class 中的单个字符,它可以是 non-digit 文字加号。您的第二个字符 class 匹配文字 2。因此您正在寻找 'A2' 或 '+2' 等模式。适当的正则表达式将取决于您的输入。


提案:

的回答中,我已经建议了一个不同的 JS-based 函数,用于替换数据而不是匹配数据。为此添加:

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

现在对两列尝试模式:

^(\d+(?:\.\d+)?)?\s*(.+)$

</code> 替换数字部分的值,用 <code> 替换剩余的单位。

let
    Source = Excel.CurrentWorkbook(){[Name="Tabel1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    #"Invoked Custom Function1" = Table.AddColumn(#"Changed Type", "Nr", each fnRegexExtr([Column1], "^(\d+(?:\.\d+)?)?\s*(.+)$", "")),
    #"Invoked Custom Function2" = Table.AddColumn(#"Invoked Custom Function1", "Unit", each fnRegexExtr([Column1], "^(\d+(?:\.\d+)?)?\s*(.+)$", "")),
    #"Replaced Errors" = Table.ReplaceErrorValues(#"Invoked Custom Function2", {{"Nr", null}, {"Unit", null}})
in
    #"Replaced Errors"

第二个选项是用分隔符替换该值,您稍后会在以下位置拆分:

let
    Source = Excel.CurrentWorkbook(){[Name="Tabel1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    #"Invoked Custom Function" = Table.AddColumn(#"Changed Type", "fnRegexExtr", each fnRegexExtr([Column1], "^(\d+(?:\.\d+)?)?\s*(.+)$", "|")),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Invoked Custom Function", "fnRegexExtr", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), {"Nr", "Unit"})
in
    #"Split Column by Delimiter"