Skip to content

Instantly share code, notes, and snippets.

@pixeltrix
Created May 13, 2020
Embed
What would you like to do?
Match one of three number plate patterns
formats = [
"[A-Z]{2}[0-9]{2}[A-Z]{3}",
"[A-Z]{1}[0-9]{3}[A-Z]{3}",
"[A-Z]{3}[0-9]{3}[A-Z]{1}"
]
pattern = /\A#{formats.join("|")}\z/
candidates = %w[
RF10AK0
RF1OAK0
RF1OAKO
RF10AKO
]
match = candidates.detect(-> { "No Match" }) do |candidate|
candidate.match?(pattern)
end
puts "Result: #{match}"
$ ruby anpr.rb
Result: RF10AKO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment