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

  • SEARCH
  • Home
  • 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 690987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:29:35+00:00 2026-05-14T02:29:35+00:00

Following up to Regular expression to match hostname or IP Address? and using Restrictions

  • 0

Following up to Regular expression to match hostname or IP Address?
and using Restrictions on valid host names as a reference, what is the most readable, concise way to match/validate a hostname/fqdn (fully qualified domain name) in Python? I’ve answered with my attempt below, improvements welcome.

  • 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. Editorial Team
    Editorial Team
    2026-05-14T02:29:36+00:00Added an answer on May 14, 2026 at 2:29 am
    import re
    def is_valid_hostname(hostname):
        if len(hostname) > 255:
            return False
        if hostname[-1] == ".":
            hostname = hostname[:-1] # strip exactly one dot from the right, if present
        allowed = re.compile("(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE)
        return all(allowed.match(x) for x in hostname.split("."))
    

    ensures that each segment

    • contains at least one character and a maximum of 63 characters
    • consists only of allowed characters
    • doesn’t begin or end with a hyphen.

    It also avoids double negatives (not disallowed), and if hostname ends in a ., that’s OK, too. It will (and should) fail if hostname ends in more than one dot.

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

Sidebar

Related Questions

I'm using the following regular expression to match everything: /^(?=.{10,8000}$).*$/ But now I just
I'm using the following regular expression with the preg_match function in PHP to match
I don't understand, why does the following regular expression: ^*$ Match the string 127.0.0.1?
How can i match the following dates with python regular expression in one statement,
currently I'm trying to find a regular expression with the following condition: Match: FOO_.*
I'm using the following regular expression to march hrefs on a web page. \/static\/workout\/[A-Z]{1,4}032812[A-Z]{1,5}-EQB.html
I have the following regular expression : I figured out most of the part
I have the following Regular Expression which matches an email address format: ^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$ This
I have been using the following regular expression in ASP.NET and Javascript: [a-zA-ZöäüÖÄÜß0-9]{1}[a-zA-ZöäüÖÄÜß0-9_.\-]{2,14}[a-zA-ZöäüÖÄÜß0-9.!]{1} Now,
I have written the following regular expression. It should match only filenames or folder

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.