user3325376
user3325376

Reputation: 198

regex - optional word after using non-greedy

i have some urls like :

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

i am trying to get subtitle name , in this case its my-subtitle-name-3 so it should capture my-subtitle-name-3 in all of above urls

it tried this regex \/subtitles\/(.*?)\/.* so it does not match for URLs that have not any additional / at URL,

I tired make / optional at my regex so I used \/subtitles\/(.*?)\/?.* too, but it's not worked at all

here are test links :

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

https://regex101.com/r/BOIfzG/2 ==> i tried make \ optional after using non-greedy but it's not worked

The first one detects most URLs and the second one not working

Upvotes: 1

Views: 106

Answers (1)

Rehan Manzoor
Rehan Manzoor

Reputation: 2753

Here is the solution to your issue.

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

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

Thanks to @preaction

Upvotes: 2

Related Questions