# RCR: regex + regex

**URL:** <https://www.ruby-forum.com/t/rcr-regex-regex/177934>\
**Category:** Ruby\
**Created:** [October 26, 2009, 10:13pm UTC](https://www.ruby-forum.com/t/rcr-regex-regex/177934 "2009-10-26T22:13:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [October 26, 2009, 10:13pm UTC](https://www.ruby-forum.com/t/rcr-regex-regex/177934/1 "2009-10-26T22:13:46Z")

</div>

Feedback on the following suggestion for ruby:  
by default allow for adding regex’s

i.e.

> > /foo/ + /bar/  
> > =\> /foobar/

Thoughts?  
-r

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [October 26, 2009, 11:08pm UTC](https://www.ruby-forum.com/t/rcr-regex-regex/177934/2 "2009-10-26T23:08:49Z")

</div>

> What might be the corresponding \* or - behaviors? It seems like  
> a \* b  
> is closer to  
> /(a)\*(b)/

That’s what I’d guess for \*, as well.

> than anything else I could think of and that immediately implies:  
> a + b  
> becomes:  
> /(a)+(b)/  
> rather than just /(a)(b)/
> 
> The fact that + is a meaningful character in a Regexp makes a  
> universal meaning for it as an operation _on_ regexps a bit ambiguous.

Yeah, or what |  
means or what not. I’d probably just stick with concatenation and not  
even _define_ \*.

-r

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [October 26, 2009, 10:52pm UTC](https://www.ruby-forum.com/t/rcr-regex-regex/177934/3 "2009-10-26T22:52:59Z")

</div>

On Oct 26, 2009, at 5:13 PM, Roger P. wrote:

> Feedback on the following suggestion for ruby:  
> by default allow for adding regex’s
> 
> i.e.
> 
> > > /foo/ + /bar/  
> > > =\> /foobar/
> 
> ## Thoughts? -r

irb\> a=/foo/  
=\> /foo/  
irb\> b=/bar/  
=\> /bar/  
irb\> class Regexp  
irb\> def +(other)  
irb\> self.class.new(self.to\_s + other.to\_s)  
irb\> end  
irb\> end  
=\> nil  
irb\> a+b  
=\> /(?-mix:foo)(?-mix:bar)/

This is obviously too naive an implementation, but if we change a to /  
foo/i then I’d expect  
“Foobar” =~ (a+b)  
to be true (well, I mean 0, of course) and  
“fooBar” =~ (a+b)  
to be nil.

What might be the corresponding \* or - behaviors? It seems like  
a \* b  
is closer to  
/(a)\*(b)/  
than anything else I could think of and that immediately implies:  
a + b  
becomes:  
/(a)+(b)/  
rather than just /(a)(b)/

The fact that + is a meaningful character in a Regexp makes a  
universal meaning for it as an operation _on_ regexps a bit ambiguous.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [October 27, 2009, 2:04pm UTC](https://www.ruby-forum.com/t/rcr-regex-regex/177934/4 "2009-10-27T14:04:29Z")

</div>

2009/10/26 Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > becomes:  
> > /(a)+(b)/  
> > rather than just /(a)(b)/
> > 
> > The fact that + is a meaningful character in a Regexp makes a  
> > universal meaning for it as an operation _on_ regexps a bit ambiguous.
> 
> Yeah, or what |  
> means or what not. I’d probably just stick with concatenation and not  
> even _define_ \*.

You are aware that you can use string interpolation in regular  
expressions, do you?

irb(main):001:0\> a=/foo/  
=\> /foo/  
irb(main):002:0\> b=/bar/  
=\> /bar/  
irb(main):003:0\> x=/#{a}#{b}/  
=\> /(?-mix:foo)(?-mix:bar)/

Kind regards

robert
