re = /^([a-zA-Z]{2}\d{4})(?!-\d)(L\d|Transit|T$)?/m
str = 'AB1201
AB-1201
AB1201-T
AB-12-01L1
AB1201-TER
AB1201 Transit
AB1201EFH
AB1201-02
AB1201-EFH'
# 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