I would like help formulating a regex which I can query MySQL5.6 with to get a list of hostnames which are in proper FQDN format.
I found the following regex doing an online search which closely matches what I'm looking for https://www.regextester.com/98986
"^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.){2,}([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]){2,}$"
When I tested this regex in python, it gave me the results I expected.
>>> reg = "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.){2,}([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]){2,}$"
>>> hostname ='abc.tmp.company.com'
>>> print(re.search(reg,hostname))
<_sre.SRE_Match object at 0x109da5880>
>>> res = re.search(reg,hostname)
>>> res.group(0)
'abc.tmp.company.com'
>>> invalid_hostname ='invalid'
>>> print(re.search(reg,invalid_hostname))
None
>>> invalid_hostname2 = 'in-test-stg'
>>> print(re.search(reg,invalid_hostname2))
None
But when I queried MySQL, it returned names which were in non-FQDN format too i.e. single word names such as 'invalid' were returned from the DB.
select name from MYTABLE
where name RLIKE '^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.){2,}([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]){2,}$'
I've reviewed https://dev.mysql.com/doc/refman/5.6/en/regexp.html and https://www.geeksforgeeks.org/mysql-regular-expressions-regexp/ but there might be something I'm missing.
How can I get that regular expression to work in MYSQL with the RLIKE operator? Alternatively, what regular expression can I use when querying MYSQL natively to return names which are in FQDN form?