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

Time:11-23

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.

CodePudding user response:

If you only care about the first part, then

^[^.] 

should do the trick.

https://regex101.com/r/vWLPRa/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/INhd8I/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/2at2e4/1

CodePudding user response:

You can use it,

(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})
  • Related