In my application, I must read a URL and do something if the URL contains Basic authentication credentials. An example of such a URL is
http://username:password@example.com
Is the regular expression below a good fit for my task? I am to capture four groups into local variables. The URL is passed to another internal library that will do further work to ensure the URL is valid before opening a connection.
^(.+?//)(.+?):(.+?)@(.+)$
It looks ok, and I think that a regular expression is good to use in this case. A couple of suggestions:
1) I think that named groups would make your code more readable, i.e:
Then you can simply write
2) then you could make the expression a little more strict, e.g. using
\wwhen possible instead of.: