Regular expression object question

Hi,

Well, if your regular expression is not very complicated, you can
simply use the Regexp “new()” and “source()” methods:

re = Regexp.new ('xyz')
re = Regexp.new (re.source + 'abc')
....

Regards,

Bill

Good idea. What about overloading += for the source method?

e.g.

re = /xyz/
re.source += “|abc”
puts re.source → “xyz|abc”

Worth an RFC?

Regards,

Dan

···

==============================================================

Daniel Berger djberge@v55host11.interprise.com wrote:

Hi all,

Is there a way to add a new regex to an existing regular expression
object?

In other words, say I start with a pattern I want to match:

re = /xyz/

But later, I decide that I want to also match “abc”. As it
stands now,
I have to create a new regex object for that. Thus:

re2 = /abc/

This could go on indefinitely, so simply doing this could
quickly become
cumbersome:

if re.match(string) || re2.match(string)…

Thus, what I would like is a way to “push” a new regex onto
an old one
(i.e. add an “and” or “or”).

example:

re = /xyz/
re.push(“|abc”) → re is now /xyz|abc/

Thoughts? Should I just create my own subclass that does
this and shut
up?

Regards,

Dan

Hi,

I don’t know whether it is worth an RFC (what is RFC?)

At least, regarding the understanding, when you want to overload +=, it is
not for the source method, but for the regexp itself. “re.source” returns
a new string, which is independent of the re. Because a string accepts
the “+” operator, “re.source += str” returns another new string, but the
original re does not change. The correct way for your intention (when
it is overloaded) is to write “re += ‘|abc’”. By the same token, you just
want a regexp to be able to accept the “+” operator (which is actually a
method).

Regards,

Bill

···

=============================================================================
Berger, Daniel djberge@qwest.com wrote:

Good idea. What about overloading += for the source method?

e.g.

re = /xyz/
re.source += “|abc”
puts re.source → “xyz|abc”

Worth an RFC?

Regards,

Dan

irb(main):001:0> /ab/+“cd”
NameError: undefined method `+’ for /ab/:Regexp
from (irb):1
irb(main):002:0> class Regexp
irb(main):003:1> def +(str)
irb(main):004:2> Regexp.new(source + str)
irb(main):005:2> end
irb(main):006:1> end
nil
irb(main):007:0> /ab/+“cd”
/abcd/

Seems reasonable. But then you would also want to test for a regex
argument (and you can’t just apply #to_s), and possibly other arguments.

Is there a reason this doesn’t already exist?

William Djaja Tjokroaminata wrote:

···

Hi,

I don’t know whether it is worth an RFC (what is RFC?)

At least, regarding the understanding, when you want to overload +=, it is
not for the source method, but for the regexp itself. “re.source” returns
a new string, which is independent of the re. Because a string accepts
the “+” operator, “re.source += str” returns another new string, but the
original re does not change. The correct way for your intention (when
it is overloaded) is to write “re += ‘|abc’”. By the same token, you just
want a regexp to be able to accept the “+” operator (which is actually a
method).

Regards,

Bill

Berger, Daniel djberge@qwest.com wrote:

Good idea. What about overloading += for the source method?

e.g.

re = /xyz/
re.source += “|abc”
puts re.source → “xyz|abc”

Worth an RFC?

Regards,

Dan


Joel VanderWerf California PATH, UC Berkeley
mailto:vjoel@path.berkeley.edu Ph. (510) 231-9446
http://www.path.berkeley.edu FAX (510) 231-9512

I don’t know the reasons but I can imagine

  • Other #+ hardly raise but Regexp#+ does easily: /ab/ + “|”

  • source+Regexp::escape(str) is sefe but too rigid

  • Options wounld not be preserved: /<./m + “>” != /<.>/m

  • Ruby 1.7 has Regexp#options but what is reasonable //mu + //nx

  • What should happen: “” + //

– Gotoken

···

At Wed, 25 Sep 2002 06:57:01 +0900, Joel VanderWerf wrote:

irb(main):001:0> /ab/+“cd”
NameError: undefined method `+’ for /ab/:Regexp
from (irb):1
irb(main):002:0> class Regexp
irb(main):003:1> def +(str)
irb(main):004:2> Regexp.new(source + str)
irb(main):005:2> end
irb(main):006:1> end
nil
irb(main):007:0> /ab/+“cd”
/abcd/

Seems reasonable. But then you would also want to test for a regex
argument (and you can’t just apply #to_s), and possibly other arguments.

Is there a reason this doesn’t already exist?