Regular Expression for validating DNS label ( host name)

后端 未结 6 1306
长发绾君心
长发绾君心 2020-12-03 21:11

I would like to validate a hostname using only regualr expression.

Host Names (or \'labels\' in DNS jargon) were traditionally defined by RFC 952 and RFC 1123 and m

6条回答
  •  旧巷少年郎
    2020-12-03 21:31

    Ruby regular expressions are multiline by default, and so something like Rails warns against using ^ and $. This is Mark's answer with safe start- and end of string characters:

    \A(?![0-9]+$)(?!-)[a-zA-Z0-9-]{,63}(?

提交回复
热议问题