RCR: regex + regex

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

i.e.

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

Thoughts?
-r

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

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
[email protected]

2009/10/26 Roger P. [email protected]:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs