Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify Regexp options using Regexp.union

Tags:

syntax

regex

ruby

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.

like image 455
steenslag Avatar asked Oct 02 '12 18:10

steenslag


2 Answers

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
like image 61
pje Avatar answered Sep 23 '22 18:09

pje


schemes_regex = Regexp.union(
  *URI.scheme_list.keys
  .map{|s| Regexp.new(s, Regexp::IGNORECASE)}
)
like image 24
sawa Avatar answered Sep 26 '22 18:09

sawa