Regular Expression that validates a list of port numbers (1-65535) across multiple lines

Viewed 40

I am looking for a regular expression to match valid port numbers (1-65535) listed in a text area. The input will look like this:

80
80
25
53
110
---
---

and so on, so I need an expression that is able to handle validation in this way. Across multiple lines, unlimited times, be it that the user will be able to list as many port numbers as they need.. I have already taken a crack at writing my own expression and also using and expression I found.

^((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9]{4})|([0-5]{0,5})|([0-9]{1,4}))$

which only does about half of what I need. Validates the number perfectly. but breaks as soon white space or a new line is created. Help would be greatly appreciated. Thanks.

1 Answers

Did you try to add the whitespace and new lines to the expression? something like that:

^(((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9]{4})|([0-5]{0,5})|([0-9]{1,4}))[\s\r\n]+)+$
Related