From: Christophe Broult Date: 2001-09-14T06:14:29+09:00 Subject: [ruby-talk:21187] RE: variables in reg exp repetition This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. ------_=_NextPart_001_01C13C98.F73CFF70 Content-Type: text/plain > -----Original Message----- > From: Paul Rubel [mailto:rubel@crhc.uiuc.edu] > Sent: Thursday, September 13, 2001 5:11 PM > To: ruby-talk@ruby-lang.org > Subject: [ruby-talk:21186] variables in reg exp repetition > > > Hello, > I've been working on some code lately and have come upon some strange > behavior (at least I think so). > Is there a problem with variables in repetition in regexps? > > ruby 1.6.4 (2001-06-04) [i686-linux] > /bin/ruby> irb > irb(main):001:0> three = 3 > 3 > irb(main):002:0> 'aaa' =~ /[a]{3,3}/ > 0 > irb(main):003:0> 'aaa' =~ /[a]{three,three}/ > nil > irb(main):004:0> > You need to use #{} to access the variable in the regex like you would do in a string: irb(main):005:0> 'aaa' =~ /[a]{#{three},#{three}}/ 'aaa' =~ /[a]{#{three},#{three}}/ 0 will do what you want. Christophe > thank you, > Paul > ------_=_NextPart_001_01C13C98.F73CFF70 Content-Type: text/html RE: [ruby-talk:21186] variables in reg exp repetition

> -----Original Message-----
> From: Paul Rubel [mailto:rubel@crhc.uiuc.edu]
> Sent: Thursday, September 13, 2001 5:11 PM
> To: ruby-talk@ruby-lang.org
> Subject: [ruby-talk:21186] variables in reg exp repetition
>
>
> Hello,
>  I've been working on some code lately and have come upon some strange
> behavior (at least I think so).
>  Is there a problem with variables in repetition in regexps?
>
> ruby 1.6.4 (2001-06-04) [i686-linux]
> /bin/ruby> irb
> irb(main):001:0> three = 3
> 3
> irb(main):002:0> 'aaa' =~ /[a]{3,3}/
> 0
> irb(main):003:0> 'aaa' =~ /[a]{three,three}/
> nil
> irb(main):004:0>
>

You need to use #{} to access the variable in the regex like you would do in a string:

irb(main):005:0> 'aaa' =~ /[a]{#{three},#{three}}/
'aaa' =~ /[a]{#{three},#{three}}/
0

will do what you want.

Christophe
>   thank you,
>         Paul
>

------_=_NextPart_001_01C13C98.F73CFF70--