正则表达式:对 url 字符串的两个斜杠之间的倒数第二个值进行子串 [英] Regex: Substring the second last value between two slashes of a url string

查看:34
本文介绍了正则表达式:对 url 字符串的两个斜杠之间的倒数第二个值进行子串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的字符串:

I have a string like this:

http://www.example.com/value/1234/different-value

如何提取1234?

注意:末尾可能有斜线:

Note: There may be a slash at the end:

http://www.example.com/value/1234/different-value
http://www.example.com/value/1234/different-value/

推荐答案

/([^/]+)(?=/[^/]+/?$)

应该可以.您可能需要根据您使用的语言对其进行不同的格式化.例如,在 Ruby 中,它是

should work. You might need to format it differently according to the language you're using. For example, in Ruby, it's

if subject =~ /\/([^\/]+)(?=\/[^\/]+\/?\Z)/
    match = $~[1]
else
    match = ""
end

这篇关于正则表达式:对 url 字符串的两个斜杠之间的倒数第二个值进行子串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆