可选字符匹配正则表达式

时间:2018-03-01 23:25:55

标签: regex perl optional-values

我的条件可能是我的字符串:

  1. test23 @ testbee:在/ var /蜂/
    1. testbee:/var/bee/test.html
    2. 我需要在这里提取三个字符串:test23(如果可用),testbee和test.html

      所以,在Perl中,

      ($user, $sys, $file) = ($source =~ /(\S*?)\@?(\S+?):?[^:]*?([^\/]+)$/);
      

      for 1. this give,$ user =,$ sys = test @ testbee,$ file = test.html for 2. give,$ user =,$ sys = test,$ file = test.html

      有没有办法,我可以使用一个表达式来获取用户'test'(如果它存在),如果不存在则没有。

2 个答案:

答案 0 :(得分:2)

更易于维护的解决方案:

select T.number, max(T.MOST_FREQUENT) max_val, W.MOST_FREQUENT, W.Type
  from (

  SELECT *, SUM(quantity) AS MOST_FREQUENT
       FROM datas
       WHERE date_status=1
       GROUP BY number,type
       ORDER BY SUM(quantity) DESC LIMIT 200 ) T 
  INNER JOIN (
  SELECT *, SUM(quantity) AS MOST_FREQUENT
           FROM datas
           WHERE date_status=1
           GROUP BY number,type
           ORDER BY SUM(quantity) DESC LIMIT 200
  ) W ON T.number = W.number 
  group by T.number, , W.MOST_FREQUENT, W.Type
  ORDER BY  concat(lpad(max(T.MOST_FREQUENT), 10, '0'), T.number)  DESC
           , (concat(max(T.MOST_FREQUENT, T.number))= concat(W.MOST_FREQUENT, T.number)) DESC
           ,  W.MOST_FREQUENT
           , W.Type

答案 1 :(得分:1)

此处总是将可选部分与第1组匹配,有时$1$3将为空:

(?:(\w+)@)?(\w+):(?:\/\w+){2}\/((?:\w|\.)*)

演示:regex101

如果您想跳过更多子目录,请将{2}替换为*