RCR: regex + regex

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

i.e.

/foo/ + /bar/

=> /foobar/

Thoughts?
-r

···

--
Posted via http://www.ruby-forum.com/\.

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

i.e.

/foo/ + /bar/

=> /foobar/

Thoughts?
-r
--

a=/foo/

=> /foo/

b=/bar/

=> /bar/

class Regexp
  def +(other)
    self.class.new(self.to_s + other.to_s)
    end
  end

=> nil

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 Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

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

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

···

--
Posted via http://www.ruby-forum.com/\.

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

···

2009/10/26 Roger Pack <rogerpack2005@gmail.com>:

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* *.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/