$re = '/^(?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+)/m';
$str = 'OpenVPN CLIENT LIST
Updated,Thu Nov 11 10:00:15 2021
20.20.20.20,n.y.f_o_o,2.2.2.2:11111 Thu Nov 11 10:00:14 2021
10.10.10.10,a.a.foo,3.3.3.3:22222,Thu Nov 11 10:00:14 2021
Updated,Thu Nov 11 10:00:12 2021
Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since
30.30.30.30,t.p.bar,192.168.1.1:58348,Thu Nov 11 10:00:12 2021
40.40.40.40,test,192.168.1.11:58348,Thu Nov 11 10:00:02 2021
50.50.50.50,test_2,33.33.33.3:58348,Thu Nov 11 10:00:22 2021';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
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 PHP, please visit: http://php.net/manual/en/ref.pcre.php