Regular expression object question

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,

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

···

==========================================================================
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

Hello Daniel,

Tuesday, September 24, 2002, 7:56:55 PM, you wrote:

re = /xyz/

re2 = /abc/

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

see eregex.rb. more effective approach:

class Array
def =~ (str)
find {|re| re =~ str}
}
}

list = [/abc/, /xyz/]
list << /def/ << /tuv/
print ‘Yes!’ if list =~ ‘qwerty’

btw, you don’t need to subclass Regexp just to implement one it’s
operator :wink:

···


Best regards,
Bulat mailto:bulatz@integ.ru