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'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
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 Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html