Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,155,814 members, 7,827,987 topics. Date: Tuesday, 14 May 2024 at 08:48 PM

Regex To Get Domain Name From Url - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Regex To Get Domain Name From Url (238 Views)

How To Remove Category From URL Permalink / Get Wordpress Website/blog With Free Domain Name, Hosting At 5k / What Is The URL For Jumia Seller Centre API (2) (3) (4)

(1) (Reply) (Go Down)

Regex To Get Domain Name From Url by 3exe3: 10:18am On Mar 28, 2023
"(http(s):\/\/)?(www\.)?([a-zA-z]+\.){0,}(\w)+)"

this is the regex i wrote but i have no match if i do

example.com
ab.example.com
www.example.com

1 Like

Re: Regex To Get Domain Name From Url by truthCoder: 1:52pm On Mar 28, 2023
3exe3:
"(http(s):\/\/)?(www\.)?([a-zA-z]+\.){0,}(\w)+)"

this is the regex i wrote but i have no match if i do

example.com
ab.example.com
www.example.com

^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n]+)

Explanation:
^ matches the beginning of the string
(?:https?:\/\/)? is a non-capturing group that matches http:// or https:// if they are present
(?:[^@\n]+@)? is a non-capturing group that matches the username and password (if any) that might be present in the URL
(?:www\.)? is a non-capturing group that matches the www. prefix (if any)
([^:\/\n]+) is a capturing group that matches the domain name, which is any sequence of characters that are not a colon (smiley, a forward slash (/), or a newline character (\n)

Source: chatGPT

Test this and revert.

1 Like

Re: Regex To Get Domain Name From Url by airsaylongcome: 4:28pm On Mar 28, 2023
truthCoder:


^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n]+)

Explanation:
^ matches the beginning of the string
(?:https?:\/\/)? is a non-capturing group that matches http:// or https:// if they are present
(?:[^@\n]+@)? is a non-capturing group that matches the username and password (if any) that might be present in the URL
(?:www\.)? is a non-capturing group that matches the www. prefix (if any)
([^:\/\n]+) is a capturing group that matches the domain name, which is any sequence of characters that are not a colon (smiley, a forward slash (/), or a newline character (\n)

Source: chatGPT

Test this and revert.

I swear CGPT is like cheat mode in a game. I love it.

3 Likes

Re: Regex To Get Domain Name From Url by 3exe3: 5:32pm On Mar 28, 2023
truthCoder:


^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n]+)

Explanation:
^ matches the beginning of the string
(?:https?:\/\/)? is a non-capturing group that matches http:// or https:// if they are present
(?:[^@\n]+@)? is a non-capturing group that matches the username and password (if any) that might be present in the URL
(?:www\.)? is a non-capturing group that matches the www. prefix (if any)
([^:\/\n]+) is a capturing group that matches the domain name, which is any sequence of characters that are not a colon (smiley, a forward slash (/), or a newline character (\n)


thanks will test it
Source: chatGPT

Test this and revert.
Re: Regex To Get Domain Name From Url by truthCoder: 5:48pm On Mar 28, 2023
airsaylongcome:


I swear CGPT is like cheat mode in a game. I love it.
I swear.

(1) (Reply)

Sports Bets Selection Exchange: Developers, Investors, Brainstormers Jump In / Suitecommerce Advanced: A Revolutionary E-commerce Development Solution With SDF / Who Is Shola Akinlade, The Man Who Revolutionalize Africa Payments

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 11
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.