re = /^[\s\S]+?(?<!\d)(\d{8})(?!\d)/
str = 'Test123456789 -- should fail because 9 digits
Test23456789Test -- pass
Test23456789 Test -- pass
13456780Test -- pass
Test0123456 -- fail because 7 digits
Extra text in the email: I’ve attached the information you requested. If you have any questions, please let us know. -- extra text in the email shouldn\'t matter.'
# Print the match result
str.match(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