Regular expression to match DNS hostname or IP Address?

后端 未结 21 2535
长发绾君心
长发绾君心 2020-11-21 07:25

Does anyone have a regular expression handy that will match any legal DNS hostname or IP address?

It\'s easy to write one that works 95% of the time, but I\'m hoping

相关标签:
21条回答
  • 2020-11-21 08:07

    Here is a regex that I used in Ant to obtain a proxy host IP or hostname out of ANT_OPTS. This was used to obtain the proxy IP so that I could run an Ant "isreachable" test before configuring a proxy for a forked JVM.

    ^.*-Dhttp\.proxyHost=(\w{1,}\.\w{1,}\.\w{1,}\.*\w{0,})\s.*$
    
    0 讨论(0)
  • 2020-11-21 08:08

    I thought about this simple regex matching pattern for IP address matching \d+[.]\d+[.]\d+[.]\d+

    0 讨论(0)
  • 2020-11-21 08:10
    /^(?:[a-zA-Z0-9]+|[a-zA-Z0-9][-a-zA-Z0-9]+[a-zA-Z0-9])(?:\.[a-zA-Z0-9]+|[a-zA-Z0-9][-a-zA-Z0-9]+[a-zA-Z0-9])?$/
    
    0 讨论(0)
提交回复
热议问题