[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Concatenation of regular expressions

skhisma

3/27/2006 6:10:00 PM

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][S]?[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

6 Answers

Ara.T.Howard

3/27/2006 6:19:00 PM

0

Logan Capaldo

3/27/2006 6:25:00 PM

0


On Mar 27, 2006, at 1:13 PM, Geoff wrote:

> 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][S]?[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][S]?[1-9][\d]*)[\s]/

Just be careful with the string literal parts of the regexp


Andrew Johnson

3/27/2006 6:35:00 PM

0

On 27 Mar 2006 10:09:41 -0800, Geoff <skhisma@gmail.com> wrote:
> 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

--
Andrew L. Johnson http://www.s...
What have you done to the cat? It looks half-dead.
-- Schroedinger's wife

skhisma

3/27/2006 6:37:00 PM

0

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

dblack

3/27/2006 6:39:00 PM

0

skhisma

3/27/2006 6:49:00 PM

0

> 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