【问题标题】:Remove a sequence of pipe separated numbers using regex使用正则表达式删除一系列管道分隔的数字
【发布时间】:2021-05-23 16:04:34
【问题描述】:

我正在尝试匹配由字符串中的管道分隔的四个数字的序列。数字可以是负数、浮点数或两位数,例如:

13|5|-1|35|5|0|313|4|1.5|1

字符串还可以包含额外的数字和单词;一个完整的例子如下所示:

SOME STRING CONTENT 13|5|-1|3 MORE 1.6 CONTENT HERE

如何使用正则表达式识别管道左/右之间的那些数字?

我尝试了[\d\-.\|],它匹配所有数字、小数、管道和负号,但也发现它匹配字符串中的附加数字/小数内容。任何有关仅选择该部分的帮助将不胜感激!

【问题讨论】:

    标签: regex string regex-lookarounds


    【解决方案1】:

    你可以使用

    -?\b\d+(?:\.\d+)?(?:\|\-?\d+(?:\.\d+)?){3}\b
    

    模式匹配:

    • -? 匹配一个可选的-
    • \b防止部分匹配的单词边界
    • \d+(?:\.\d+)? 匹配 1+ 个数字和可选的小数部分
    • (?:\|\-?\d+(?:\.\d+)?){3} 重复 3 次,与前面的部分相同,前面有一个管道
    • \b一个字边界

    Regex demo

    【讨论】:

    • 您回答这个问题的速度令人印象深刻!非常感谢。您能解释一下您当前的答案与之前的\b\-?\d+(?:\.\d+)?(?:\|\-?\d+(?:\.\d+)?){3}\b 之间的区别吗?
    • @jclark754 我添加了各个部分的细分。
    • 你走得太快了:),在时间限制允许的情况下接受。再次,非常感谢。
    【解决方案2】:

    也可以使用

    (?<!\S)-?\d*\.?\d+(?:\|-?\d*\.?\d+){3}(?!\S)
    

    proof

    解释

    --------------------------------------------------------------------------------
      (?<!                     look behind to see if there is not:
    --------------------------------------------------------------------------------
        \S                       non-whitespace (all but \n, \r, \t, \f,
                                 and " ")
    --------------------------------------------------------------------------------
      )                        end of look-behind
    --------------------------------------------------------------------------------
      -?                       '-' (optional (matching the most amount
                               possible))
    --------------------------------------------------------------------------------
      \d*                      digits (0-9) (0 or more times (matching
                               the most amount possible))
    --------------------------------------------------------------------------------
      \.?                      '.' (optional (matching the most amount
                               possible))
    --------------------------------------------------------------------------------
      \d+                      digits (0-9) (1 or more times (matching
                               the most amount possible))
    --------------------------------------------------------------------------------
      (?:                      group, but do not capture (3 times):
    --------------------------------------------------------------------------------
        \|                       '|'
    --------------------------------------------------------------------------------
        -?                       '-' (optional (matching the most amount
                                 possible))
    --------------------------------------------------------------------------------
        \d*                      digits (0-9) (0 or more times (matching
                                 the most amount possible))
    --------------------------------------------------------------------------------
        \.?                      '.' (optional (matching the most amount
                                 possible))
    --------------------------------------------------------------------------------
        \d+                      digits (0-9) (1 or more times (matching
                                 the most amount possible))
    --------------------------------------------------------------------------------
      ){3}                     end of grouping
    --------------------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
    --------------------------------------------------------------------------------
        \S                       non-whitespace (all but \n, \r, \t, \f,
                                 and " ")
    --------------------------------------------------------------------------------
      )                        end of look-ahead
    

    【讨论】:

      猜你喜欢
      • 2014-05-31
      • 2013-06-22
      • 2021-08-09
      • 1970-01-01
      • 2023-03-15
      • 2020-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多