Simon Breton
Simon Breton

Reputation: 2876

How to match the first / after specific character

I've been looking at multiple questions but couldn't find an answers... even though I'm sure that my issue is easy to solve....

I have the following pattern :

https://www.website.domain/blablabla/my-page-path-here

I would like to capture website.domain. I've been playing with https://regex101.com/, and I've come up with this until now :

www.(.*)(\/.*?)

but doesn't work...

Thanks !

Upvotes: 0

Views: 41

Answers (2)

gribvirus74
gribvirus74

Reputation: 765

Use positive lookbehind: (?<=www\.)[^\/]+

Explanation:
(?<=www\.) - matches next expression if it's preceded by www.
[^\/]+ - matches one or more any characters excluding /

Upvotes: 2

Jonathan Newton
Jonathan Newton

Reputation: 840

Close but this might be a bit better

(?!w{1,}\.)(\w+\.?)([a-zA-Z]+)(\.\w+) Match: "website.domain"

It selects the domain and sub domain and ignores the www

It you want to keep the www just drop the first group

(\w+\.?)([a-zA-Z]+)(\.\w+) Match: www.website.domain

Upvotes: 0

Related Questions