re = /^(?:\+44\s?|0)[1238]\d\s?(?:\d\s?){7,8}$/m
str = '# Standard
01386 444400
01386444400
+441386444400
+44 1386 444400
+44 1386444400
# 5 Digit
01386 41204
0138641204
+44138641204
+44 1386 41204
+44 138641204
# 3 Digit area codes (e.g. London)
+442074343046
02074343046
020 74343046
020 7434 3046
# 4 Digit area codes (e.g. Leeds)
+441133971337
01133971337
0113 3971337
# Commercial
08451772266
0845 1772266
# Mobile number (should not match)
07999888777
+447999888777
07999 888 777
+44 7999888777
'
# 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