The way to test if a string is a valid email address is to directly use URI::MailTo::EMAIL_REGEXP
require 'uri'
puts URI::MailTo::EMAIL_REGEXP.match?('me@example.com') # true
puts URI::MailTo::EMAIL_REGEXP.match?('me@example.com ') # false (trailing space)
However, the correct way to validate that a string is a valid URL is to wrap the regex in anchors:
require 'uri'
puts /\A#{URI::RFC2396_PARSER.make_regexp}\z/.match?('https://example.com/') # true
puts /\A#{URI::RFC2396_PARSER.make_regexp}\z/.match?('https://example.com/ ') # false (trailing space)
If I directly use make_regexp, that second string matches because of a partial match.
require 'uri'
puts URI::RFC2396_PARSER.make_regexp.match?('https://example.com/ ') # true (trailing space)
Same behavior if I pass schemes into make_regexp or use the too. I'm on Ruby 4.0.1 but I don't think this behavior changed recently.
Would it be okay if I made a PR adding a keyword argument to make_regexp? It would get called like:
URI::RFC2396_PARSER.make_regexp(full_match: true).match?('https://example.com/ ') # true (trailing space)
I figured changing the default behavior could be a breaking change for a lot of people and that doesn't feel worth it.
The way to test if a string is a valid email address is to directly use
URI::MailTo::EMAIL_REGEXPHowever, the correct way to validate that a string is a valid URL is to wrap the regex in anchors:
If I directly use make_regexp, that second string matches because of a partial match.
Same behavior if I pass schemes into
make_regexpor use the too. I'm on Ruby 4.0.1 but I don't think this behavior changed recently.Would it be okay if I made a PR adding a keyword argument to
make_regexp? It would get called like:I figured changing the default behavior could be a breaking change for a lot of people and that doesn't feel worth it.