繁体   English   中英

仅在 perl 正则表达式中匹配最后一次出现

[英]Match last occurrence only in perl regex

/1.1/s/1/-/g

我正在做学校作业以参考实现 sed 命令。 我得到这个字符串来匹配“/1/-/”。 我有实验

$str =~ m{/[^/]*/[^/]*/}g;

但结果是/1.1/s/。 我怎样才能只得到“/1/-/”有人可以帮我吗?

利用

/[^/]*/[^/]*/(?=[^/]*$)

证明

解释

--------------------------------------------------------------------------------
  /                        '/'
--------------------------------------------------------------------------------
  [^/]*                    any character except: '/' (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  /                        '/'
--------------------------------------------------------------------------------
  [^/]*                    any character except: '/' (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  /                        '/'
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    [^/]*                    any character except: '/' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
--------------------------------------------------------------------------------
  )                        end of look-ahead

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM