Match last occurrence only in perl regex

Viewed 121
/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?

1 Answers

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
Related