开发者

hostname regex help

开发者_运维百科

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜