I am trying to use this regex
(\d{4}-\d{2}-\d{2})\s(\d{2}\:\d{2}\:\d{2})\.\d\s([A-Z]{3})((\d{1,3}\.){3}\d{1,3}(?:(:\d{1,5})?))\s?(.*)
to match a string such as
2021-05-20 11:03:00.0 GMT222.111.222.33
and
2021-05-20 11:03:00.0 GMT222.111.222.33:2323
In the case of this string:
2021-05-20 11:03:00.0 GMT222.111.222.33:2323444 first second
When I enter a port number with more than 5 digits, the extra digits are automatically included with the next matching group 44 first second
instead of not matching the entire string at all. The port number is meant to be optional (matching it with the IP group if it exists)
How may I fix this?
CodePudding user response:
I'm not sure I fully understand the problem but this could be the solution:
(\d{4}-\d{2}-\d{2})\s(\d{2}\:\d{2}\:\d{2})\.\d\s([A-Z]{3})((\d{1,3}\.){3}\d{1,3}(?:(:\d{1,})?))\s?(?>.*?)
CodePudding user response:
This is the fix:
^(\d{4}-\d{2}-\d{2})\s(\d{2}\:\d{2}\:\d{2})\.\d\s([A-Z]{3})((\d{1,3}\.){3}\d{1,3}((:\d{1,5})?))$
Example: https://regex101.com/r/oF7HI2/1