Reputation: 80065
In "How do I removing URLs from text?" the following code is suggested:
require 'uri'
#...
schemes_regex = /^(?:#{ URI.scheme_list.keys.join('|') })/i
#...
I tried to improve this to:
schemes_regex = Regexp.union(URI.scheme_list.keys)
but I can't figure out how the IGNORECASE
option (i
) should be specified.
Upvotes: 8
Views: 1153
Reputation:
same as sawa's response but it is looking for scheme to be at the beginning of string:
Regexp.union(*URI.scheme_list.keys.map {|s| /\A#{s}/i })
Upvotes: 2
Reputation: 168091
schemes_regex = Regexp.union(
*URI.scheme_list.keys
.map{|s| Regexp.new(s, Regexp::IGNORECASE)}
)
Upvotes: 3
Reputation: 22697
I don't believe it's possible to pass option arguments to Regexp.union
like that. You could of course specify them after the union operation:
require 'uri'
Regexp.new(Regexp.union(URI.scheme_list.keys).source, Regexp::IGNORECASE)
# => /FTP|HTTP|HTTPS|LDAP|LDAPS|MAILTO/i
Upvotes: 4