Logan Capaldo
3/27/2006 6:25:00 PM
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