import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?P<dst_ip>\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})?,?(?P<User>[a-zA-Z._\\d]+),(?P<src_ip>\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):(?<ID>\\d+)";
final String string = "OpenVPN CLIENT LIST\n"
+ "Updated,Thu Nov 11 10:00:15 2021\n"
+ "20.20.20.20,n.y.f_o_o,2.2.2.2:11111 Thu Nov 11 10:00:14 2021\n"
+ "10.10.10.10,a.a.foo,3.3.3.3:22222,Thu Nov 11 10:00:14 2021\n"
+ "Updated,Thu Nov 11 10:00:12 2021\n"
+ "Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since\n"
+ "30.30.30.30,t.p.bar,192.168.1.1:58348,Thu Nov 11 10:00:12 2021\n"
+ "40.40.40.40,test,192.168.1.11:58348,Thu Nov 11 10:00:02 2021\n"
+ "50.50.50.50,test_2,33.33.33.3:58348,Thu Nov 11 10:00:22 2021";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html