簡體   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