【问题标题】:Simple regex excel extraction简单的正则表达式excel提取
【发布时间】:2016-11-23 12:41:00
【问题描述】:

我得到了数千行的 excel,例如:

Basic medical supply - AIT - needs documents from trasnport - drugs
Basic medical supply - TTT - needs documents from trasnport - patiens and other stuff
random string from excel - TTP - other text like always - marijuana per rectum

如何设置正则表达式以在第三个“-”字符之后获取字符串。在这个例子中,“drugs”、“patiens and other stuff”和“marijuana perrectum”。

它不能在 jQuery 等中完成。需要纯 JS,因为我在 Pentago BI 中编写脚本。这个字符串非常随机,只有 3 个“-”字符是常量。有可能做这样的事情吗?也许将所有内容都放入第三个“ - ”切片,然后将所有内容保存到变量中并修剪空格和非字母字符?

编辑:

我刚刚注意到,在此列中每隔一行的文件中就有产品代码:

550-1008-000000-405.02.04.03

我也必须在 e.q 550-1028 和 405.02.04.03 上拆分,所以我必须删除字符串中间的那些零并获得 2 个子字符串

【问题讨论】:

  • 您可以使用我们的正则表达式对每个字符串进行尝试(“基本医疗用品 - AIT - 需要来自运输部门的文件 - 药物”).split("-")[3].trim();跨度>
  • costName = Hospital.getString();成本名称 = 成本名称.toString(); costName = costName.split("-)[3].trim();
  • 不工作应该是因为与下一行混合但它更接近;)谢谢

标签: javascript regex pentaho kettle


【解决方案1】:

正则表达式可能只是/^.+-.+-.+-\s*([^-]+)\s*$/

甚至更简单:/-\s*([^-]+)\s*$/

最后一个“-”之后的所有内容。或者该字符串本身是否包含“-”?

【讨论】:

    【解决方案2】:

    以下正则表达式可以解决问题:

    ^([^-]+-){3}(.*)
    

    结果在$2 中,因此独立代码 sn-p 如下所示:

    var s = [
              'Basic medical supply - AIT - needs documents from trasnport - drugs'
            , 'Basic medical supply - TTT - needs documents from trasnport - patiens and other stuff'
            , 'random string from excel - TTP - other text like always - marijuana per rectum'
        ]
      , res
      , i;
    
    for ( i=0; i < s.length; i++ ) {
        res = s[i].replace(/^([^-]+-){3}(.*)/g, "$2");
        console.log ( "#" + i + ": '" + s[i] + "# -> '" + res + "'\n" );
    }
    

    live test 在正则表达式 101 上。

    说明

    正则表达式基于除- 之外的任意字符序列,后跟单个-。它匹配此碱基序列的 3 次连续出现,并将该行的其余部分分配给捕获组 2。

    警告

    注意连续的 - 字符 - 此解决方案与此类字符串不兼容,并且处理可能取决于您的数据的正确方法(例如,-- 可能作为破折号 的 ascii 表示出现? )。

    【讨论】:

      【解决方案3】:

      如果你真的想要一个正则表达式,你可以使用这个:

      var s = 'Basic medical supply - AIT - needs documents from trasnport - drugs';
      var regex = /[^-]+-[^-]+-[^-]+-(.*)/;
      var match = regex.exec(s);
      console.log(match[1]); //outputs "drugs"
      

      但我更喜欢@VinodLouis(在 cmets 中)的解决方案,而不使用正则表达式...

      【讨论】:

      • 几乎所有的东西都有效,但我在编辑中写了另一个必须包含的东西
      • @Touche 就像您在其他答案中看到的那样(也忽略了产品问题),之后添加其他条件不是很明智。我建议您删除编辑文本并打开另一个问题。
      【解决方案4】:

      这很简单,无需任何时间使用正则表达式

      var s = [
                'Basic medical supply - AIT - needs documents from trasnport - drugs'
              , 'Basic medical supply - TTT - needs documents from trasnport - patiens and other stuff'
              , 'random string from excel - TTP - other text like always - marijuana per rectum'
          ];
      
      s.forEach(function(el){
        console.log(el.split("-")[3].trim());
      });

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-17
        • 2021-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多