http://qs321.pair.com?node_id=1196571


in reply to Complicated regexp problem

/ (?: # non capture group start ftp:\/{2} # ftp:// )? # end optional non-capture group (?: # non capture group start .*?\/ # anything non-greedy, followed by a / ){5} # end non-capture group, match five times (.*?) # capture everything, non-greedy \/ # until the very next fwd slash /x

Note that you can stringify that into a single line and put it back into the variable. The x modifier allows you to have whitespace in the regex for clarity, and adding comments.