I am trying to create a regex expression that match something that is not formatted as : ip|port.
A port value can be between [1, 65535].
Examples of set of data:
(1) 8.8.8.8|0 (bad: port 0 not allowed)
(2) 8.8.8.8|1 (good)
(3) 8.8.8.8|65536 (bad: port > 65535)
(4) 8.8.8.8|dawda (bad: char)
The regex expression (match bad data) should match (1), (3) and (4).
Consider that the ip part will always be right (no need to regex it) and what I need to check is the port. Because of that, I started the evaluation by the end of the line like this:
Regex to match a port between 0 and 65535:
\|(6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3})
Regex with end of line matching:
\|(6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3})$
Now, I want to negate it to catch line that dosent end with a valid port. I look in other forums (How to negate specific word in regex?, Regular Expressions and negating a whole character group) and learn about negative lookahead regex.
According to those forums and negative lookahead regex, my regex should be as:
^(?!(MY_REGEX)).*$
I modified my regex and added .* for the ip part to plug the ^.
Negative regex at end of line:
^(?!.\|(6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3})).$
The problem I have is the ending part .*$ which allow something after the port number. In the end, this code will be executed with PHP. According to PHP, variable length look-behind is not supported, which make me choose lookahead regex in first place.
Thanks for the help.