Search code examples
regexpcre

RegEx Match only first part of URL and use this as variable


Always been struggeling with RegEx, help is much appreciated! I want to match parts of a URL with Regex but cannot get my head around it.

Domains are:

https://name.secondpart.thirdpart.com

I want my regex to match

name How would I achieve this?

Started with (?<=^|\.) and (?<=^|\.)secondpart\.thirdpart\.com$ but it didn't work.


Solution

  • Note that I escaped the slashes using leading backslashes, assuming you use slash as your regex delimiter. If you use no or a different delimiter (e.g. #) you can just use / instead of \/.


    If you only care about the first part, then

    ^[^.:\/]+:\/\/([^.]+)
    

    should do the trick.

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


    If you for also need to enforce a specific domain after that then capture it in a group like this

    ^[^.:\/]+:\/\/([^.]+)\.secondpart\.thirdpart\.com$
    

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


    If you want to make sure that at exactly 3 parts come after the sub-domain part, but don't care what they are:

    ^[^.:\/]+:\/\/([^.]+)(?:\.[^.]+){3}$
    

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