简体   繁体   中英

Match last occurrence only in perl regex

/1.1/s/1/-/g

I am working on school assignment to reference implementation sed command. I get this string to match "/1/-/". I have experiment

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

but result is /1.1/s/. How can I get "/1/-/" only Could somebody help me?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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