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.
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 (eg #) 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}$
As you tagged pcre, you can use:
^https?://\K[^\s./]+
Explanation
^
Start of string https?://
Match the protocol with an optional s \K
Forget what is matched so far [^\s./]+
Match 1+ times a non whitespace char except .
and /
See a regex demo .
You can use it,
(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.