1

I have this hostname regex that I'd like to expand on a bit. It will match hostnames such as:

yahoo.com

mail.yahoo.com

but will also match

&^%yahoo.com etc...

(\w+\.\w+\.\w+)

Can someone tell me what I need to add to only allow letters, numbers and periods?

jim
  • 23
  • 2

3 Answers3

0

Maybe if you tried Google. Regular expression to match DNS hostname or IP Address?

Community
  • 1
  • 1
tangrs
  • 9,709
  • 1
  • 38
  • 53
0

Try using [a-zA-Z0-9\.]+ instead of \w+.

So all in all this will be:

([a-zA-Z0-9\.]+\.[a-zA-Z0-9\.]+\.[a-zA-Z0-9\.]+)
Dennis Röttger
  • 1,975
  • 6
  • 32
  • 51
  • Thanks Dennis, that works. I'll credit you when the wait period ends. – jim Apr 29 '11 at 12:36
  • @jim, why do you want to match something like "............." ? Maybe you want to use a online test tool like this here: [http://www.rubular.com/r/4AQ9ASZJpc](http://www.rubular.com/r/4AQ9ASZJpc) – stema Apr 29 '11 at 12:42
0

\w should not match &^%. I think you are missing anchors.

Try \b, that matches a word boundary.

\b\w+\.\w+\.\w+\b

But hopefully you are aware that this regex is very simplified? There has been already some question and answers to that topic, e.g. 1141848/regex-to-match-url

Community
  • 1
  • 1
stema
  • 90,351
  • 20
  • 107
  • 135
  • Thanks stema. I didn't have the anchors as you suggested. I'll give this a try now – jim Apr 29 '11 at 12:39