# Is it possible to do multiple regex in one operation?

**URL:** <https://www.ruby-forum.com/t/is-it-possible-to-do-multiple-regex-in-one-operation/178024>\
**Category:** Ruby\
**Created:** [October 28, 2009, 6:24am UTC](https://www.ruby-forum.com/t/is-it-possible-to-do-multiple-regex-in-one-operation/178024 "2009-10-28T06:24:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [October 28, 2009, 6:24am UTC](https://www.ruby-forum.com/t/is-it-possible-to-do-multiple-regex-in-one-operation/178024/1 "2009-10-28T06:24:33Z")

</div>

Hello,

I’m trying to grep some of the content from a file which contains  
character ; or { or } or “while” or “function”

in shell, it similar to;  
cat file.txt | grep -e “;” -e “{” -e “}” -e “while” -e "function

I’ve tried to use operation like below;  
output.each {|f| puts f if f =~ /[;{}]/}  
however, I failed to add another 2 keyword which are “while” and  
“function”

I was wonder if multiple regex can be evaluate in one operation like  
/[;{}]/,/while/,/function/ or  
/[;{}]/ || /while/ || /function/  
or any other forms

Thanks in advance

Ahmad A.,

---

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [October 28, 2009, 6:39am UTC](https://www.ruby-forum.com/t/is-it-possible-to-do-multiple-regex-in-one-operation/178024/2 "2009-10-28T06:39:11Z")

</div>

On Wed, Oct 28, 2009 at 1:24 PM, Ahmad A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> /[;{}]/,/while/,/function/ or  
> /[;{}]/ || /while/ || /function/

try

/;|{|}|while|function/

kind regards -botp

---

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [October 28, 2009, 6:10pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-do-multiple-regex-in-one-operation/178024/3 "2009-10-28T18:10:36Z")

</div>

On Wed, Oct 28, 2009 at 1:24 AM, Ahmad A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hello,
> 
> I’m trying to grep some of the content from a file which contains  
> character ; or { or } or “while” or “function”

This should work

/[;{}]|while|function/

use ‘|’ or operator to separate ‘while’ and ‘function’ and place ; { }  
inside the set clause [] for single match, make sense?

> Thanks in advance
> 
> ## Ahmad A.,
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–  
Kind Regards,  
Rajinder Y.

[http://DevMentor.org](http://DevMentor.org)

Do Good! - Share Freely, Enrich and Empower people to Transform their  
lives.

---

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [November 9, 2009, 7:39pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-do-multiple-regex-in-one-operation/178024/4 "2009-11-09T19:39:43Z")

</div>

Yes, both ways did working for me, thanks for the inputs. At first I  
can’t imaging regex does have ‘and’ or ‘or’ condition in it. But now I  
believe

Really appreciate that
