Regular expression extraction part URL

I’m lazy tonight and don’t want to figure this out. I need a regular expression to match’jeremy.miller’ and’scottgu’ in the following input:

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 good job of meeting the above requirements. What if these are inputs, then you can’t use ‘archive’ in regular expressions?

http://codebetter.com/blogs/jeremy.miller/
http://weblogs.asp.net/scottgu/

will this be what you want?

'/([^/]+)/archive/'

In both cases, capture the Block. According to the style of regular expression, you need to escape /s to make it work. As an alternative, if you don’t want to match the archive part, you can use lookahead, but I don’t like lookahead, and it is easier to match a lot and only capture The part you need (in my opinion), so if you want to use the forward look to verify whether the next part is archived, you can write one yourself.

Edit: When you update your question, I am right The idea of ​​what you want becomes vague. If you want a new regular expression to match the second case, you can use the same conditions as before and select the corresponding part at the end:

'/([^/]+)/$'

If you specifically need the text jeremy.miller or scottgu, no matter where they appear in the URL , But just the “word” in the URL (that is, not scottgu2), please use/warn to try again:

'/(jeremy\.miller|scottgu)/'

'/(jeremy\.miller|scottgu)/' 

As a third option, if you want the field after the domain name, unless the field is "blog", it will become furry, especially / caveat:

'http://[^/]+/(?:blogs/)?([^/]+)/'

This will match the domain name, the optional blog field, and then Match the required field. The (?:) syntax is a non-capturing group, which means it is like regular parentheses, but does not capture the value, so the only value captured is the value you want. (?:) According to you The specific regex expresses the flavor and there is a risk of change. I don’t know what language you are asking for, but I mainly use Perl, so if you use PCRE, then this regular expression should be a lot. If you use something different, please check Non-capturing group.

Wow. This is a lot of discussion about regular expressions. I need to shut up and post.

I am lazy tonight, Don't want to figure this out. I need a regular expression to match'jeremy.miller' and'scottgu' in the following input:

http://codebetter. com/blogs/jeremy.miller/archive/2009/08/26/talking-about-storyteller-and-executable-requi rements-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 good job of meeting the above requirements. What if these are inputs, then you can’t use ‘archive’ in regular expressions?

http://codebetter.com/blogs/jeremy.miller/
http://weblogs.asp.net/scottgu/

Will this be what you want?

'/([^/]+)/archive/'

In both cases, capture the Block. According to the style of regular expression, you need to escape /s to make it work. As an alternative, if you don't want to match the archive part, you can use lookahead, but I don't like lookahead, and it is easier to match a lot and only capture The part you need (in my opinion), so if you want to use the forward look to verify whether the next part is archived, you can write one yourself.

Edit: When you update your question, I am right The idea of ​​what you want becomes vague. If you want a new regular expression to match the second case, you can use the same conditions as before and select the corresponding part at the end:

'/([^/]+)/$'

If you specifically need the text jeremy.miller or scottgu, no matter where they appear in the URL , But just the "word" in the URL (that is, not scottgu2), please use/warn to try again:

'/(jeremy\.miller|scottgu)/'

'/(jeremy\.miller|scottgu)/' 

As a third option, if you want the field after the domain name, unless the field is "blog", it will become furry, especially / caveat:

'http://[^/]+/(?:blogs/)?([^/]+)/'

This will match the domain name, the optional blog field, and then Match the required field. The (?:) syntax is a non-capturing group, which means it is like regular parentheses, but does not capture the value, so the only value captured is the value you want. (?:) According to you The specific regex expresses the flavor and there is a risk of change. I don’t know what language you are asking for, but I mainly use Perl, so if you use PCRE, then this regular expression should be a lot. If you use something different, please check Non-capturing group.

Wow. This is a lot of discussion about regular expressions. I need to shut up and post.

Leave a Comment

Your email address will not be published.