Regular Expression for text that validates IPV4 or Hostname followed by a port number eg: 127.0.0.1:8080 or localhost:8080 An attempt with regular expression to do a foolproof validation of an IV4 or Hostname:port Number.

Example texts that can be validated for any of the following:



www.google.com:8080
127.0.0.1:8000
localhost:65535
codereview.stackexchange.com:9000
www.n-plus-t.com:4000
30my_domain.test-ng.com:754



((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?![\\d]) | ([\\p{Alnum}+ &amp;&amp; [.{1}-_*[\p{Alnum}+]]]){1,253}:(6553[0-5]|655[0-2][0-9]\d|65[0-4](\d){2}|6[0-4](\d){3}|[1-5](\d){4}|[1-9](\d){0,3})


The IPv4 address validations are mainly to ensure that the tuples are between the range 0 to 255.  Also the hostname is validated against the valid names which can be alphanumeric, starting with number, subdomains separated by a single . and - and _ allowed in between, though not at end.  Port range is validated from 0 to 65535
While this appears to work, there are a few things about it that are not very clear.

Reconsider the use of regex

Rather than using a regex, for the numeric ranges in particular, it may make more sense to capture those values and check them with Java statements.  It would certainly reduce the size and complexity of the regex.

Document non-obvious constraints

The current regex includes a 1,253 count, the origin of which is decidedly non-obvious.  RFC 1035 and RFC 822 are both obvious candiates for an authoritative source, but this regex does not conform to either of those.