re = /\b((?:AAA|BBC|CCC)(?=\d{3}\b)|(?:ZZ|XX|YY)(?=\d{2}\b))/
str = 'AAA031 but not AAA00 and match ZZ00
AAA031
AAA0312
AAA02
AAA0
ZZ00
ZZ211'
# 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