首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >regex -使用非贪婪后的可选词

regex -使用非贪婪后的可选词
EN

Stack Overflow用户
提问于 2017-09-21 23:42:21
回答 1查看 103关注 0票数 1

我有一些网址,如:

代码语言:javascript
复制
http://exmple.com/subtitles/my-subtitle-name-3
http://exmple.com/subtitles/my-subtitle-name-3/arabic
http://exmple.com/subtitles/my-subtitle-name-3/danish
http://exmple.com/subtitles/my-subtitle-name-3/farsi_persian
http://exmple.com/subtitles/my-subtitle-name-3?wdwrf
http://exmple.com/subtitles/my-subtitle-name-3?wdwrf=ok&nep=1
http://exmple.com/subtitles/my-subtitle-name-3/arabic/
http://exmple.com/subtitles/my-subtitle-name-3/danish/
http://exmple.com/subtitles/my-subtitle-name-3/farsi_persian/
http://exmple.com/subtitles/my-subtitle-name-3/farsi_persian/?sdsf
http://exmple.com/subtitles/my-subtitle-name-3/farsi_persian/?sdsf&dfe=pl

http://exmple.com/subtitles/my-subtitle-name-3/arabic/1627077
http://exmple.com/subtitles/my-subtitle-name-3/danish/1629022
http://exmple.com/subtitles/my-subtitle-name-3/farsi_persian/1593665
http://exmple.com/subtitles/my-subtitle-name-3/arabic/1627077/
http://exmple.com/subtitles/my-subtitle-name-3/danish/1629022//
http://exmple.com/subtitles/my-subtitle-name-3/farsi_persian/1593665/?1xds

我正在尝试获取字幕名,在本例中是它的my-subtitle-name-3,因此它应该在所有上述urls中捕获my-subtitle-name-3

它尝试了这个regex \/subtitles\/(.*?)\/.*,这样它就不匹配在URL上没有任何附加/的URL,

我厌倦了让/在我的正则表达式上是可选的,所以我也使用了\/subtitles\/(.*?)\/?.*,但是它根本不起作用

下面是测试链接:

https://regex101.com/r/BOIfzG/1

https://regex101.com/r/BOIfzG/2 ==>在使用非贪婪之后,我尝试使\成为可选的,但它没有工作。

第一个检测到大多数的URL,第二个不工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-21 23:51:45

这是解决你的问题的办法。

/subtitles/([^/?\s]+)

https://regex101.com/r/VSzC0u/2

感谢@preaction

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46354771

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档