steenslag
steenslag

Reputation: 80065

How to specify Regexp options using Regexp.union

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

Answers (3)

user904990
user904990

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 })

See live demo here

Upvotes: 2

sawa
sawa

Reputation: 168091

schemes_regex = Regexp.union(
  *URI.scheme_list.keys
  .map{|s| Regexp.new(s, Regexp::IGNORECASE)}
)

Upvotes: 3

pje
pje

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

Related Questions