简体   繁体   English

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

[英]Match last occurrence only in perl regex

/1.1/s/1/-/g

I am working on school assignment to reference implementation sed command.我正在做学校作业以参考实现 sed 命令。 I get this string to match "/1/-/".我得到这个字符串来匹配“/1/-/”。 I have experiment我有实验

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

but result is /1.1/s/.但结果是/1.1/s/。 How can I get "/1/-/" only Could somebody help me?我怎样才能只得到“/1/-/”有人可以帮我吗?

Use利用

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

See proof .证明

EXPLANATION解释

--------------------------------------------------------------------------------
  /                        '/'
--------------------------------------------------------------------------------
  [^/]*                    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