| Computers Forum Index » Computer Languages (Ruby) » RCR: regex + regex... |
|
Page 1 of 1 |
|
| Author |
Message |
| Roger Pack... |
Posted: Tue Oct 27, 2009 1:13 am |
|
|
|
Guest
|
Feedback on the following suggestion for ruby:
by default allow for adding regex's
i.e.
Quote: /foo/ + /bar/
=> /foobar/
Thoughts?
-r
--
Posted via http://www.ruby-forum.com/. |
|
|
| Back to top |
|
|
|
| Rob Biedenharn... |
Posted: Tue Oct 27, 2009 1:52 am |
|
|
|
Guest
|
On Oct 26, 2009, at 5:13 PM, Roger Pack wrote:
Quote: 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 Biedenharn http://agileconsultingllc.com
Rob at (no spam) AgileConsultingLLC.com |
|
|
| Back to top |
|
|
|
| Roger Pack... |
Posted: Tue Oct 27, 2009 2:08 am |
|
|
|
Guest
|
Quote: 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.
Quote: 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/. |
|
|
| Back to top |
|
|
|
|