Greedy and Nongreedy Matching in a Regular Expression

19 November, 2011 - 1 min read

This questions has come to me many times so it is time to write a post that acts as a reminder.

Currently I have a string like

ftp://user:[email protected]/dirA/dirB/file

and what i want is parse it to get the user, password, server and path to the file (/dirA/dirB/file). My first try was:


but that returns me server=server/dirA/dirB, which isn't what I want. The idea is that the group after the @ would make a non gready match. This is achieved using the ? char. So the final and right regular expression will becomes:


which returns server=server and file=/dirA/dirB/file.

© I built this site withGatsbyfrom the starterjuliaand made a bunch of modifications. The full content is available in myrepository. Icons made byFreepik from Flaticon