简体   繁体   English

正则表达式在最后两个斜杠之间获取文本

[英]Regex to get text between final two slashes

Taking a string with the following three examples... 使用以下三个示例获取字符串...

/Services/Research/Consumer-services/Mobile-Services/
/Services/Research/Consumer-services/Fixed-Broadband-and-Multi-Play/
/Services/Research/Consumer-services/Next-Generation-Services/

I need to: 我需要:

a) Strip so I only have the string contained between the two final slashes (ie. "Mobile-Services") ... /.*/(.*)/ seems to accomplish this, though I don't fully understand ti. a)剥离,所以我只有两个斜杠之间包含字符串(即“ Mobile-Services”)... /.*/(.*)/似乎可以完成此操作,尽管我并不完全理解ti。

b) Replace dashes in the remaining text with spaces. b)将其余文本中的破折号替换为空格。 So, "Mobile-Services" becomes "Mobile Services" and "Fixed-Broadband-and-Multi-Play" becomes "Fixed Broadband and Multi Play" 因此,“移动服务”变为“移动服务”,“固定宽带和多重播放”变为“固定宽带和多重播放”

Thank-you very much. 非常感谢你。

You can use this regex to capture last part: 您可以使用此正则表达式捕获最后一部分:

/([^/]+)/?$

And use captured group #1 并使用捕获的组#1

RegEx Demo 正则演示

You can use following regex for get the last string : 您可以使用以下正则表达式获取最后一个字符串:

/(\w+-\w+)/$

The anchor $ will matches right after the last character in the string. 锚点$将匹配字符串中最后一个字符之后的位置。

Demo 演示版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM