# Regexp.union - set option all patterns case insensitive

**URL:** <https://www.ruby-forum.com/t/regexp-union-set-option-all-patterns-case-insensitive/132191>\
**Category:** Ruby\
**Created:** [March 17, 2008, 7:50pm UTC](https://www.ruby-forum.com/t/regexp-union-set-option-all-patterns-case-insensitive/132191 "2008-03-17T19:50:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [March 17, 2008, 7:50pm UTC](https://www.ruby-forum.com/t/regexp-union-set-option-all-patterns-case-insensitive/132191/1 "2008-03-17T19:50:05Z")

</div>

I want to pull a list of terms and use them to scan documents to  
determine if any of those words are present.

Let’s say fruits = [“apples”,“oranges”,“grapes”].

I do Regexp.union(\*fruits). This returns /apples|oranges|grapes/ which  
is good.

Now I want this pattern to be case insensitive, i.e. -  
/apples|oranges|grapes/i

Docs say I can set each argument to case insensitive and then call union  
e.g. - Regexp.union(/dogs/, /cats/i) #=\>  
/(?-mix:dogs)|(?i-mx:cats)/

I have been doing fruits.map!{|fruit| Regexp.new(fruit,  
Regexp::IGNORECASE)}; Regexp.union(\*fruits)

This seems to work. Is there any better or cleaner way to do this?

Thanks,

John

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [March 17, 2008, 8:16pm UTC](https://www.ruby-forum.com/t/regexp-union-set-option-all-patterns-case-insensitive/132191/2 "2008-03-17T20:16:15Z")

</div>

On Mar 17, 12:49 pm, John H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## John
> 
> Posted viahttp://www.ruby-forum.com/.

I don’t know how much better this is, but you could union first, then  
add the case-insensitivity:

fruits = [“apples”,“oranges”,“grapes”]  
r = Regexp.union(\*fruits)  
r = Regexp.new(r.source, Regexp::IGNORECASE) # =\> /apples|oranges|  
grapes/i

HTH,  
Chris

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [April 10, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/regexp-union-set-option-all-patterns-case-insensitive/132191/3 "2008-04-10T16:05:40Z")

</div>

Chris S. wrote:

> I don’t know how much better this is, but you could union first, then  
> add the case-insensitivity:
> 
> fruits = [“apples”,“oranges”,“grapes”]  
> r = Regexp.union(\*fruits)  
> r = Regexp.new(r.source, Regexp::IGNORECASE) # =\> /apples|oranges|  
> grapes/i

This solution works. Thanks.

Looking at the Ruby source, it wouldn’t be hard to add an options  
parameter to Regexp.union to avoid such re-conversions.
