RCR: regex + regex

R

Roger Pack

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

i.e.=> /foobar/

Thoughts?
-r
 
R

Rob Biedenharn

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

i.e.
=> /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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
R

Roger Pack

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
 
R

Robert Klemme

2009/10/26 Roger Pack said:
That's what I'd guess for *, as well.


Yeah, or what |
means or what not. =A0I'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=3D/foo/
=3D> /foo/
irb(main):002:0> b=3D/bar/
=3D> /bar/
irb(main):003:0> x=3D/#{a}#{b}/
=3D> /(?-mix:foo)(?-mix:bar)/

Kind regards

robert

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top