2016-06-21 98 views
-1

我有一个字符串,它看起来像这样:在正则表达式查询返回的特定值匹配

5 Secs (14.2725%) 60 Secs (12.630%) 300 Secs (15.5993%) 

使用(\d{2}[.]\d{3}),我可以匹配我想要的值;但是,我只需要一个查询的值为1,另一个查询的值为2,第三个值为3。这是监控系统的一部分,因此必须使用单一的正则表达式来完成,我没有访问其他shell工具可以使这一点变得简单。

+1

什么是你真正想从你给的例子中提取?顺便说一句,我认为你将需要摆脱你提到的正则表达式中的一堆东西。 –

+3

几乎没有关于什么编程语言或shell的信息。另外,如果你有一个shell,为什么你说“我没有访问其他shell工具”?通常在系统上安装大量的shell工具。也许你可以填写我们的细节? – Kusalananda

+0

一个几乎相同的问题:http://stackoverflow.com/questions/37924545/return-the-next-nth-result-w-after-a-hyphen-globally/37924922#37924922 –

回答

0

说明
^(?:[^(]*\([^)]*\)){2}[^(]*\(\s*\K[0-9]{2}[.][0-9]{3} 
        ^^^ 

Regular expression visualization

{2}的数量可以根据需要而改变,然后将表达将选择该串中的N + 1%的值。如果选择0,那么表达式将匹配第一个百分比,如果您选择1则表达式将匹配第二个百分比...等等。

该表达式假设语言支持\K正则表达式命令,该命令强制引擎删除与之相匹配的所有内容,直到\K

现场演示

https://regex101.com/r/oX0mJ4/1

示例文本

5 Secs (14.2725%) 60 Secs (12.630%) 300 Secs (15.5993%) 

样品匹配

使用写入的表达式会返回第3个条目。

15.599 

说明

NODE      EXPLANATION 
---------------------------------------------------------------------- 
^      the beginning of a "line" 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (2 times): 
---------------------------------------------------------------------- 
    [^(]*     any character except: '(' (0 or more 
          times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    \(      '(' 
---------------------------------------------------------------------- 
    [^)]*     any character except: ')' (0 or more 
          times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    \)      ')' 
---------------------------------------------------------------------- 
){2}      end of grouping 
---------------------------------------------------------------------- 
    [^(]*     any character except: '(' (0 or more times 
          (matching the most amount possible)) 
---------------------------------------------------------------------- 
    \(      '(' 
---------------------------------------------------------------------- 
    \s*      whitespace (\n, \r, \t, \f, and " ") (0 or 
          more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    \K      'K' 
---------------------------------------------------------------------- 
    [0-9]{2}     any character of: '0' to '9' (2 times) 
---------------------------------------------------------------------- 
    [.]      any character of: '.' 
---------------------------------------------------------------------- 
    [0-9]{3}     any character of: '0' to '9' (3 times) 
----------------------------------------------------------------------