Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 30933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:31:32+00:00 2026-05-10T13:31:32+00:00

Given the URL (single line): http://test.example.com/dir/subdir/file.html How can I extract the following parts using

  • 0

Given the URL (single line):
http://test.example.com/dir/subdir/file.html

How can I extract the following parts using regular expressions:

  1. The Subdomain (test)
  2. The Domain (example.com)
  3. The path without the file (/dir/subdir/)
  4. The file (file.html)
  5. The path with the file (/dir/subdir/file.html)
  6. The URL without the path (http://test.example.com)
  7. (add any other that you think would be useful)

The regex should work correctly even if I enter the following URL:

http://test.example.com/example/example/example.html 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T13:31:32+00:00Added an answer on May 10, 2026 at 1:31 pm

    A single regex to parse and breakup a full URL including query parameters and anchors e.g.

    https://www.google.com/dir/1/2/search.html?arg=0-a&arg1=1-b&arg3-c#hash

    ^((http[s]?|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(.*)?(#[\w\-]+)?$

    RexEx positions:

    url: RegExp[‘$&’],

    protocol:RegExp.$2,

    host:RegExp.$3,

    path:RegExp.$4,

    file:RegExp.$6,

    query:RegExp.$7,

    hash:RegExp.$8

    you could then further parse the host (‘.’ delimited) quite easily.

    What I would do is use something like this:

    /*     ^(.*:)//([A-Za-z0-9\-\.]+)(:[0-9]+)?(.*)$ */ proto $1 host $2 port $3 the-rest $4 

    the further parse ‘the rest’ to be as specific as possible. Doing it in one regex is, well, a bit crazy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.