0

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.

6
  • I need an idea for Mysql Commented Aug 24, 2016 at 8:10
  • Works for me: regex101.com/r/hL6aD0/1 Commented Aug 24, 2016 at 8:18
  • @TomDoodler It matches whole first part of the string. I need to filter out the fqdn as per the criteria. Only 1,2,3,4 should get matched. Commented Aug 24, 2016 at 8:20
  • So add hyphen [^.-]+. Commented Aug 24, 2016 at 8:21
  • What is fqdn? Never heard that Commented Aug 24, 2016 at 8:21

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Your solution matches fqdn like this "test1bw.test.example.net". After number there should be only one alphabet. There can be hyphen after that alphabet. This should not be matched
@GaneshBabu Fixed. I suggest you add "test1bw.test.example.net" or some other test strings to your question to make your criteria clearer.
1
[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/

7 Comments

Thanks.. It works now. How to ignore rest of the domain part. ie., instead of test.example.net, there may be any names
You can replace the ".test.example.net" in the regex with your domain, or otherwise you can just use [a-z]+[0-9]+[ab](-d){0,1}(.)+
Thanks.. It works fine. But I am getting matched for examples of this form "test01.test90a-d.example.net". As per my case, I should match only first part of the domain name
[a-z]+[0-9]+[ab](-d){0,1}[.][^.]+[.][^.]+[.][^.]{2,4} would work, but is not a beautiful answer. Update: [a-z]+[0-9]+[ab](-d){0,1}[.][^.]+(.example.net) this would also work and i think it's better, but i'm not an expert.
Thanks again.. But still your new solution didn't work.. :(
|
0

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.

Comments

0
[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.

6 Comments

Thanks. But it didn't work for examples of this form "test01.test90a-d.example.net". I need a match for part before first dot.
I´ll try to fix it
what about (?<!.)[a-zA-Z]+[\d]+[a-zA-Z]+[-\w]* like here: regex101.com/r/pF3mG5/4
Lookbehind is not supported in javascript
Sorry, my mistake... this is not applicatable in js... looking for another solution...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.