Regex to extract subdomain from URL?

后端 未结 7 2439
名媛妹妹
名媛妹妹 2020-12-05 15:07

I have a bunch of domain names coming in like this:

http://subdomain.example.com (example.com is always example.com, but the subdomain varies).

I need \"subd

7条回答
  •  不思量自难忘°
    2020-12-05 15:34

    Purely the subdomain string (result is $1):

    ^http://([^.]+)\.domain\.com
    

    Making http:// optional (result is $2):

    ^(http://)?([^.]+)\.domain\.com
    

    Making the http:// and the subdomain optional (result is $3):

    (http://)?(([^.]+)\.)?domain\.com
    

提交回复
热议问题