Ganesh Babu
Ganesh Babu

Reputation: 3670

Regex matching first part of domain name

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

1.test02a-d.test.example.net
2.test02b-d.test.example.net
3.test03a.test.example.net
4.test03b.test.example.net
5.test04-d.test.example.net
6.test05.test.example.net

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

[^.]+

Criteria: 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.

Upvotes: 0

Views: 1427

Answers (4)

ltux
ltux

Reputation: 253

MySQL version:

^[^.]+[0-9]+[ab](-[^.]+)?[[:>:]]

JavaScript version:

^[^.]+[0-9]+[ab](-[^.]+)?\b

regex101.com doesn't support MySQL flavor regexp, so I only give the JavaScript version. test1bw.test.example.net 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

Upvotes: 1

BingBingBong
BingBingBong

Reputation: 88

[a-zA-Z]+[\d]+[a-zA-Z]+.*

workes for me... try it here matches the whole domain.

[a-zA-Z]+[\d]+[a-zA-Z]+[-\w]*

matches only the first part of the domain.

Upvotes: 0

SamWhan
SamWhan

Reputation: 8332

Try

^[^.]+\d[ab][^.]*

It'll match, from the start of a line (^) any characters (more than one) not being a dot. Then there must be a digit followed by an a or a b, Optionally followed by another sequence of characters not being a dot.

See it here at regex101.

Upvotes: 0

andii1997
andii1997

Reputation: 259

[a-z]+[0-9]+[ab](-d){0,1}(.test.example.net)

This Regex matches your first four domain names. The "[a-z]+" could also be specified with "(test)" You can test it on this site: http://regexr.com/

Upvotes: 1

Related Questions