0

I wish to set up a user input check for domain names. I found the following on stackoverflow Check for a valid domain name in a string?, and the answer: r'[a-zA-Z\d-]{,63}(.[a-zA-Z\d-]{,63})*' seems to suits my needs (in concept) but when I test it using re.match and a string greater than 63 characters it still returns 'true'.

When I test the result using group() it returns the first 63 characters, which makes sense. Based on that can someone please tell me what I'm doing wrong? Is Match the correct operation to use in this case or is there something else I need to do to so the string is correctly tested?

Thank you.

Community
  • 1
  • 1
user788462
  • 1,085
  • 2
  • 15
  • 24
  • Wow, that is a *terrible* regular expression for matching domains. It will literally match any word in this sentence. Don't use that regex, it's worthless. – Kurt McKee Dec 11 '11 at 21:47
  • Are you using match? Or one of the other methods? You missed an important detail from the question you are mentioning: There needs to be a \ in front of the dot. A simple dot means 'any character', a dot with a backslash in front means 'a dot'. Therefore the correct expressions is `r'[a-zA-Z\d-]{,63}(\.[a-zA-Z\d-]{,63})*'`. Maybe this already answers your question. Otherwise please post some code to clarify your question. – fqxp Dec 11 '11 at 21:57
  • @frankp Well picked up, thanks for that. Typo's gotta love em. – user788462 Dec 11 '11 at 22:34

2 Answers2

2

You should anchor it against your input and make sure the "." really matches a period.

r'^[a-zA-Z\d-]{,63}(\.[a-zA-Z\d-]{,63})*$'
David K. Hess
  • 16,632
  • 2
  • 49
  • 73
  • 1
    I modified it a bit: `r'^[a-zA-Z\d-]{,63}(\.[a-zA-Z\d-]{,63}).$'` (Note the dot(.) instead of the asterisk(*) at the end). This forces the test to have .xxx at the end. Also allows for technically correct domains with a trailing dot(.). – D.A Dec 16 '12 at 02:33
1

You can try this:

^[a-z0-9]([a-z0-9-]+\.){1,}[a-z0-9]+\Z

examples:

  • w3.example.com [match]
  • example.com [match]
  • w3.site-example.com [match]
  • -w3.example.com [not match]
  • example.buzz [match]
  • .com [not match]
  • EXAMPLE.com [not match]
Amauche
  • 45
  • 1
  • 7