zero-width positive lookahead. s![^/]+(?=/)$!_bla_! finds non-slashes, looks ahead to make sure they are followed by a slash, then checks if they are followed by a newline or end of string. That can't happen. You can't have something that's followed both by a slash and by a newline, and you can't have something that's followed both by a slash and nothing.