javajoemorgan Posted May 29, 2008 Share Posted May 29, 2008 Suppose I have a URL:[url="http://www.thisandthat.com/this/and/that"]http://www.thisandthat.com/this/and/that[/url]I want to capture everything after "http://www.thisandthat.com/", so that I have "this/and/that"However, the regular expression:"http://.*/(.*)"Captures "that" into $1. How do I make the first .* capture only up to the slash following the "www.thisandthat.com"? Link to comment Share on other sites More sharing options...
Ingolme Posted May 29, 2008 Share Posted May 29, 2008 Maybe http://[^/]*/(.*) Link to comment Share on other sites More sharing options...
jesh Posted May 29, 2008 Share Posted May 29, 2008 .* does its match in a "greedy" way, meaning that it will match as many characters as possible. So, in your example, the first .* matches "www.thisandthat.com/this/and". If you only want it to match up to the first slash, you'll want to do a "lazy" match. "http://.*?/(.*)" Link to comment Share on other sites More sharing options...
javajoemorgan Posted May 29, 2008 Author Share Posted May 29, 2008 Oh boy... wouldn't have guessed that...., but it is the solution... Thankx .* does its match in a "greedy" way, meaning that it will match as many characters as possible. So, in your example, the first .* matches "www.thisandthat.com/this/and". If you only want it to match up to the first slash, you'll want to do a "lazy" match."http://.*?/(.*)" Link to comment Share on other sites More sharing options...
javajoemorgan Posted May 29, 2008 Author Share Posted May 29, 2008 Dang it.... I used the ".*?" solution, but I was so close to this with ".*[^/]", which obviously doesn't work... and now obvious why that will. Thanks for the reply anyway. Maybe http://[^/]*/(.*) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.