I'm working on a regex for validating urls in C#. Right now, the regex I need must not match other http:// but the first one inside the url. This was my first try:
(https?:\/\/.+?)\/(.+?)(?!https?:\/\/)
But this regex does not work (even removing (?!https?:\/\/)). Take for example this input string:
http://test.test/notwork.http://test
Here is my first doubt: why does not the capturing group (.+?) match notwork.http://test? The lazy quantifier should match as few times as possible but why not until the end? In this case I was certainly missing something (Firstly I thought it could be related to backtracking but I don't think this is the case), so I read this and found a solution, even if I'm not sure is the best one since it says that
This technique presents no advantage over the lazy dot-star
Anyway, that solution is the tempered dot. This is my next try:
(https?:\/\/.+?)\/((?:(?!https?:\/\/).)*)
Now: this regex is working but not in the way I would like. I need a match only when the url is valid.
By the way, I think I haven't fully understood what the new regex is doing: why the negative lookahead stays before the . and not after it?
So I tried moving it after the . and it seems that it matches the url until it finds the second-to-last character before the second http. Returning to the corrected regex, my hypothesis is that the negative lookahead is actually trying to check what's after the . already read by the regex, is this right?
Other solutions are well-accepted, but I'd firstly prefer to understand this one. Thank you.