Regex to extract part of a url
regex
Solution
Would this be what you're looking for?
'/([^/]+)/archive/'
Captures the piece before "archive" in both cases. Depending on regex flavor you'll need to escape the `/`s for it to work. As an alternative, if you don't want to match the `archive` part, you could use a lookahead, but I don't like lookaheads, and it's easier to match a lot and just capture the parts you need (in my opinion), so if you prefer to use a lookahead to verify that the next part is `archive`, you can write one yourself.
EDIT: As you update your question, my idea of what you want is becoming fuzzier. If you want a new regex to match the second cases, you can just pluck the appropriate part off the end, with the same `/` conditions as before:
'/([^/]+)/$'
If you specifically want either the text `jeremy.miller` or `scottgu`, regardless of where they occur in a URL, but only as "words" in the URL (i.e. not `scottgu2`), try this, once again with the `/` caveat:
'/(jeremy\.miller|scottgu)/'
As yet a third alternative, if you want the field after the domain name, unless that field is "blogs", it's going to get hairy, especially with the `/` caveat:
'http://[^/]+/(?:blogs/)?([^/]+)/'
This will match the domain name, an optional `blogs` field, and then the desired field. The `(?:)` syntax is a non-capturing group, which means it's just like regular parenthesis, but won't capture the value, so the only value captured is the value you want. `(?:)` has a risk of varying depending on your particular regex flavor. I don't know what language you're asking for, but I predominantly use Perl, so this regex should pretty much do it if you're using PCRE. If you're using something different, look into non-capturing groups.
Wow. That's a lot of talking about regexes. I need to shut up and post already.
Problem
I'm being lazy tonight and don't want to figure this one out. I need a regex to match 'jeremy.miller' and 'scottgu' from the following inputs: ``` http://codebetter.com/blogs/jeremy.miller/archive/2009/08/26/talking-about-storyteller-and-executable-requirements-on-elegant-code.aspx http://weblogs.asp.net/scottgu/archive/2009/08/25/clean-web-config-files-vs-2010-and-net-4-0-series.aspx ``` Ideas? Edit Chris Lutz did a great job of meeting the requirements above. What if these were the inputs so you couldn't use 'archive' in the regex? ``` http://codebetter.com/blogs/jeremy.miller/ http://weblogs.asp.net/scottgu/ ```