> s/.+\\([^\\]+)$|.+\/([^\/]+)$/$1/; May be it try to say: s/.*\\([^\\]+)$|.*\/([^\/]+)$/$1?$1:$2/e Replace the whole line with the last field if the line contain '/' or '\' functions as field separator.