Search code examples
regexurlregex-lookaroundsregex-groupregex-greedy

RegEx to match and select specific URLs


I’m on a website with these URLs;

https://flyheight.com/videos/ybb347
https://flyheight.com/videos/yb24os
https://flyheight.com/public/images/videos/793f77362f321e62c32659c3ab00952d.png
https://flyheight.com/videos/5o6t98/#disqus_thread

I need a RegEx that will only select these URLs instead

https://flyheight.com/videos/yb24os
https://flyheight.com/videos/ybb347

This is what I got so far ^(?!images$).*(flyheight.com/videos/).*


Solution

  • I'm not too sure if this is what you were looking for, but you could use the following:

    ^(?!images$).*(flyheight.com/videos/)([^/]+)$
    

    The idea is that it would match the first part that you had, then match one or more characters that is not a slash ([^/]+) .

    If you had strings that may or may not contain the / on the end (for example, you had https://flyheight.com/videos/yb24os or https://flyheight.com/videos/yb24os/), you can try the following:

    ^(?!images$).*(flyheight.com/videos/)([^/]+)/?$
    

    here are my results on regexr.

    included testing sample if asked