re = /(?i)^(?:NPO|PRO|PCR|REQ|8)+(\d{7,8})(?!(\d))/m
str = 'NPO1234567
NPO12345678
NPO123456
NPO123456789
npo1234567
npo12345678
npo123456
npo123456789
PRO1234567
PRO12345678
PRO123456
PRO123456789
pro1234567
pro12345678
pro123456
pro123456789
PCR1234567
PCR12345678
PCR123456
PCR123456789
pcr1234567
pcr12345678
pcr123456
pcr123456789
REQ1234567
REQ12345678
REQ123456
REQ123456789
req1234567
req12345678
req123456
req123456789
81234567
812345678
8123456
8123456789
'
# 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