Concatenation of regular expressions

G

Geoff

Is there a way to concatenate regular expressions in ruby? Something
like Regexp.union but without the or between expressions.

For example say i have the following regular expressions:

Set = /[\[][1-9][\d][\]]/
Field = /[F]?[1-9][\d]*/

and i want to make a regular expression to match both of them, i'd like
to be able to do something like the following:

SetThenField = Set + /([\s])*/ + Field + /[\s]/

- Geoff
 
A

ara.t.howard

Is there a way to concatenate regular expressions in ruby? Something
like Regexp.union but without the or between expressions.

For example say i have the following regular expressions:

Set = /[\[][1-9][\d][\]]/
Field = /[F]?[1-9][\d]*/

and i want to make a regular expression to match both of them, i'd like
to be able to do something like the following:

SetThenField = Set + /([\s])*/ + Field + /[\s]/

- Geoff


you could do (untested)

def re_union *res
Regexp::new res.flatten.map{|re| re.source}.join
end

regards.

-a
 
L

Logan Capaldo

Is there a way to concatenate regular expressions in ruby? Something
like Regexp.union but without the or between expressions.

For example say i have the following regular expressions:

Set = /[\[][1-9][\d][\]]/
Field = /[F]?[1-9][\d]*/

and i want to make a regular expression to match both of them, i'd
like
to be able to do something like the following:

SetThenField = Set + /([\s])*/ + Field + /[\s]/

- Geoff


Easy enough:

irb(main):010:0> SetThenField = Regexp.new(Set.to_s + '([\s])*' +
Field.to_s + '[\s]')
=> /(?-mix:[\[][1-9][\d][\]])([\s])*(?-mix:[F]?[1-9][\d]*)[\s]/

Just be careful with the string literal parts of the regexp
 
A

Andrew Johnson

Is there a way to concatenate regular expressions in ruby? Something
like Regexp.union but without the or between expressions.

Here's a little addition (bad pun) for the Regexp class:

class Regexp
def +(re)
Regexp.new self.to_s + re.to_s
end
end

The #to_s method preserves option semantics.

andrew
 
G

Geoff

I was thinking of doing it this way but it seems somewhat unruby. From
these responces though it looks like i'll have to tack some tiny method
on to regexp to achieve this, i was hoping that this functionality was
a part of regexp (or somewhere else in ruby) and that i had just missed
it.

- Geoff
 
D

dblack

Hi --

Is there a way to concatenate regular expressions in ruby? Something
like Regexp.union but without the or between expressions.

For example say i have the following regular expressions:

Set = /[\[][1-9][\d][\]]/
Field = /[F]?[1-9][\d]*/


You don't need character classes for single characters:

Set = /\[[1-9]\d\]/
Field = /FS?[1-9]\d*/
and i want to make a regular expression to match both of them, i'd like
to be able to do something like the following:

SetThenField = Set + /([\s])*/ + Field + /[\s]/

How about:

SetThenField = /#{Set}(\s)*#{Field}\s/

(This is all untested but it might point you in a helpful direction.)


David

--
David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
 
G

Geoff

You don't need character classes for single characters:
Set = /\[[1-9]\d\]/
Field = /FS?[1-9]\d*/
Makes for slightly better looking expressions. I thought this was the
case, but didn't bother to check.
How about:
SetThenField = /#{Set}(\s)*#{Field}\s/
(This is all untested but it might point you in a helpful direction.)
Seems to work actually. I had found an old groups post about using #{}
within regular expressions but didn't bother to try it because of the
way they had explained it.

irb(main):009:0> re1 = /[\w]+/
=> /[\w]+/
irb(main):010:0> re2 = /[\d]+/
=> /[\d]+/
irb(main):017:0> "Foo 123".match(/#{re1}[\s]+#{re2}/).to_s
=> "Foo 123"

Thanks,
- Geoff
 

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

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,668
Latest member
SamiraShac

Latest Threads

Top