Skip to content

URI::MailTo::EMAIL_REGEXP matches whole strings but URI::RFC2396_PARSER.make_regexp matches partial strings #226

@supersam654

Description

@supersam654

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions