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?
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\.]+)
Maybe if you tried Google. Regular expression to match DNS hostname or IP Address?
\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
精彩评论