Forum: Ruby Concatenation of regular expressions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2ce7c81002a564e0e4139bc83d21f10a?d=identicon&s=25 Geoff (Guest)
on 2006-03-27 20:16
(Received via mailing list)
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
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-03-27 20:19
(Received via mailing list)
On Tue, 28 Mar 2006, Geoff wrote:

>
>  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
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-03-27 20:25
(Received via mailing list)
On Mar 27, 2006, at 1:13 PM, Geoff wrote:

> 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
2ce7c81002a564e0e4139bc83d21f10a?d=identicon&s=25 Geoff (Guest)
on 2006-03-27 20:39
(Received via mailing list)
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
5c841628b56df3a68984986e9f095d01?d=identicon&s=25 Andrew Johnson (andrew)
on 2006-03-27 20:39
(Received via mailing list)
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-27 20:40
(Received via mailing list)
Hi --

On Tue, 28 Mar 2006, 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]*/

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 (dblack@wobblini.net)
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
2ce7c81002a564e0e4139bc83d21f10a?d=identicon&s=25 Geoff (Guest)
on 2006-03-27 20:54
(Received via mailing list)
> 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
This topic is locked and can not be replied to.