Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional trailing slash in regex

https://embed.thatsite.com?id=
https://embed.thatsite.com/?id=

Im trying to get the trailing slash as optional before ?id=

I tried

https?:\/\/embed\.([^\/]*)\.com\/\?id=([0-9]+) //works with slash only
https:\/\/embed\.([^\/]*)\.com\?id=([0-9]+) //works without slash

So, trailing slash should be optional before ?id= because embed.thatsite.com changes with both url types.Is there any way to make it working.Thankyou


2 Answers

You can make your / as optional using ?

https?:\/\/embed\.([^\/]*)\.com\/?\?id=([0-9]+)

https?:\/\/embed\. match http\\embed. or https\\embed. where s is optional ?

([^\/]*) match everything except /

\.com\/?\? match .com and optional / and ? character

id=([0-9]+) match id= and capture numeric values as many as possible

Regex Demo

like image 108
Pavneet_Singh Avatar answered Mar 12 '26 14:03

Pavneet_Singh


This should work:

https?:\/\/embed\.([^\/]*)\.com\/?\?id=([0-9]+)

Here, the slash is changed with a ? modifier, which will match the previous character (/) 0 or 1 times.

like image 27
joepd Avatar answered Mar 12 '26 16:03

joepd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!