anonymous
anonymous

Reputation: 244

regular expression for the address of domain name server

I want to create a regular expression for DNS.

My requirement is that the valid DNS should be:

  1. www.x.y
  2. www.t.x.y
  3. www.s.t.x.y

Only 4 dots(.) are allowed. I have tried this regular expression but its not working. Please suggest.

^[a-zA-Z0-9]+.[a-zA-Z0-9]+.[a-zA-z0-9]+.[a-zA-z0-9]$

Upvotes: 1

Views: 2430

Answers (4)

Matten
Matten

Reputation: 17621

A very simple regex could be something along

^www\.[a-zA-Z0-9]+\.[a-zA-Z0-9]+(\.[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)?)?$

Three points:

  • the point (.) is a special character and must be escaped (\.)
  • we need optional clauses for the last two sections.
  • your examples start with www, so you need to include this in order to use the start mark (^)

Upvotes: 1

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

try this

str.matches("www(\\.\\w+){2,4}")

Upvotes: 0

StNickolas
StNickolas

Reputation: 606

try this regex

^(www\.|)(([a-zA-Z0-9])+\.){1,4}[a-z]+$

Upvotes: 0

Related Questions