How does this Regex work? -- Example from msdn.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

It is used to match a url:
Regex r = new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/",
RegexOptions.Compiled);

My question is , what does the pair of '?' mean? Since [^/]+ will match all
the tokens except the '/' token, which means it will also match the port, how
does this pair of '?' match the port?

Thanks in advance!
 
Ryan said:
It is used to match a url:
Regex r = new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/",
RegexOptions.Compiled);

My question is , what does the pair of '?' mean? Since [^/]+ will match all
the tokens except the '/' token, which means it will also match the port, how
does this pair of '?' match the port?

Backtracking. The [^/]+ matches to the end, then the engine tries to
match the colon by backing up 'till it finds a colon character. If
that colon isn't followed by a digit, it backs up some more until it
finds another colon. And so on.
 
Back
Top