re = /^(?P<ID>[A-Z][A-Z0-9]{3})?(?P<REF1>(?:(?!\/\/).)+)(\/\/(?P<REF2>.+))?(\n?(?P<EXTRA>.+))?$/m
str = 'TEX1CNS0P5-AA//CAT-523-VID-00EOS-0
XUX PETER LAB RANDOM TEXT DM5.
TEX2BFTBSH9999SBRT2L
RATRACE201
TEX3GWS0P2-AA//D-14839048-99-3
THERE WAS 200 COALS IN HIS STOCKING.
TEX4POF OF 20/03/09//CAT342134832489
P/O:1600 PARK AVENUE'
# 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