Regex to check top domain in DNS
Today I come to a situation where I need to find if my string has .com/.edu/.au/.info/.in or similar kind of word in it or not. And while designing a regex pattern I kept following thought process..
It will have alphanumeric characters, i.e letters, numbers, underscores and periods(dot), dashes, -, followed by a period(dot) and a character-only top-level domain such as “.com”, “.info”, “.edu”,, “.in” etc.
And based on above basic reasoning I am using, alphanumeric anytime, followed by dot(period) and followed by alphabets only and at the end there must be alphabet. And my final pattern (at present)
rawPattern = r”[a-zA-Z0-9_-]*\.[a-zA-Z]*[a-zA-Z]$”
import re def checkTopDomain(domainWord): rawPattern = r"[a-zA-Z0-9_-]*\.[a-zA-Z]*[a-zA-Z]$" result = re.search(rawPattern, domainWord) return result != None