Ganesh Babu Ganesh Babu - 1 year ago 132
MySQL Question

Regex matching first part of domain name

I am trying to match regex for the Fully Qualified Domain Name (fqdn) like this:

I tried this regex to get the first part of the fqdn:


Here I need to get the list of fqdn that has alphabets 'a' or 'b' immediately after number in first part. As shown in the above example 1,2,3,4 matches this condition.
i.e., test02a, test02a-d, test 02b and test02b-d

Fqdn 5 and 6 should be ignored as per the above criteria.

How to modify regex for matching this criteria?

Note: This should be used as REGEXP in Mysql and hence some direct javascript regexes didn't work. The solution should be applicable for both javascript and mysql.

Answer Source

MySQL version:


JavaScript version:

^[^.]+[0-9]+[ab](-[^.]+)?\b doesn't support MySQL flavor regexp, so I only give the JavaScript version. is added as a test string.

[[:>]] is specific to MySQL. For JavaScript, \b should be used instead of [[:>]]. [[:>]] is a Zero-Length Assertion matching at the end of a word (position preceded by but not followed by an ASCII letter, digit, or underscore). \b matches at a position that is the start or end of a word.

MySQL doesn't support any shorthand character classes, so [[:digit:]] or [0-9] should be used instead of \d

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download