$re = '/(X(-?\d+(?:\.\d+)?))?\s*(Y(-?\d+(?:\.\d+)?))?/m';
$str = 'N14 X411 Y279.05
N15 G2 X33 Y280.4 I.001 J4.00
N16 G1 X408 Y280.4
N17 G2 X402.3 Y280.09 I-3 J2.65
N18 G1 X402.3 Y280.09
N19 G2 X402.09 Y285.79 I2.7 J2.95
N20 G1 X402.09 Y285.79
N3 G40 Y6000
N3 G40 X-6000';
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