I am compiling a regex patter to match specific commands in a debug console, one of them is:
Pattern.compile("^\\s*\\\\connect\\s+((\\d{1,3}\\.){3}\\d{1,3})(?:\\s+(\\d+))?\\s*$");
so it is possible to enter
\connect 127.0.0.1 2345
or leave the port
\connect 127.0.0.1
spaces are ignored as long as I use at last one space to separate the fields.
It works perfectly, however, while the ip goes to group 1 (which is ok), the port goes to group 3 (as written, I know).
So I wanted to use an non-capturing group for the first three parts of the ip address:
Pattern.compile("^\\s*\\\\connect\\s+((?:\\d{1,3}\\.){3}\\d{1,3})(?:\\s+(\\d+))?\\s*$");
So I thought I would get the ip in group 1 and the port in group 2.
But now the Matcher.matches() returns false for the same input… Where is the problem?
Wild guess: The problem is somewhere else.
I did a quick test:
Output:
true
true