Search

7/11/2014

regex - Non capturing group? (?:) - Stack Overflow

regex - Non capturing group? (?:) - Stack Overflow

Let me try to explain this with an example... Consider the following text: http://stackoverflow.com/ http://stackoverflow.com/questions/tagged/regex Now, if I apply the regex below over it... (http|ftp)://([^/\r\n]+)(/[^\r\n]*)? ... I would get the following result: Match "http://stackoverflow.com/" Group 1: "http" Group 2: "stackoverflow.com" Group 3: "/" Match "http://stackoverflow.com/questions/tagged/regex" Group 1: "http" Group 2: "stackoverflow.com" Group 3: "/questions/tagged/regex" But I don't care about the protocol -- I just want the host and path of the URL. So, I change the regex to include the non-capturing group (?:). (?:http|ftp)://([^/\r\n]+)(/[^\r\n]*)? Now, my result looks like this: Match "http://stackoverflow.com/" Group 1: "stackoverflow.com" Group 2: "/" Match "http://stackoverflow.com/questions/tagged/regex" Group 1: "stackoverflow.com" Group 2: "/questions/tagged/regex" See? The first group has not been captured. The parser uses it to match the text, but ignores it later, in the final result.

沒有留言: