开发者

Regular Expression to Extract String from URL

开发者 https://www.devze.com 2023-02-20 08:31 出处:网络
I am looking to extract a string from a URL. Here is an example to illustrate what I am looking for. Input URL: http://www.nba.com/bulls/stats/

I am looking to extract a string from a URL. Here is an example to illustrate what I am looking for.

Input URL: http://www.nba.com/bulls/stats/ Output : bulls

In other words, I want to be able to extract the string between the second last and last "/" in the url. I know that I can split by "/" and extract the second last term, but am looking for a cleaner regex solutio开发者_运维问答n.

Any ideas?


Try this:

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


If you must do it by regex, you could simply do this (assuming JavaScript-style regex syntax):

/\/([^\/]*)\/[^\/]*\/$/

For the sake of making it easier to understand, the .NET version would be this:

@"/([^/]*)/[^/]*/$"

However, I think the idea of splitting on / is really the right way to do this.


The following regex can do the job

http[s]?://[\w\.]+/(\w+)/.*
0

精彩评论

暂无评论...
验证码 换一张
取 消