variables in reg exp repetition 
Author Message
 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>

  thank you,
          Paul



Tue, 02 Mar 2004 05:11:24 GMT  
 variables in reg exp repetition

Quote:

> 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

This deals with substitution of variables...

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

--
Guillaume Cottenceau - http://people.mandrakesoft.com/~gc/



Tue, 02 Mar 2004 05:27:52 GMT  
 variables in reg exp repetition

Quote:
> -----Original Message-----

> Sent: Thursday, September 13, 2001 5:11 PM

> 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

- Show quoted text -

Quote:
>   thank you,
>      Paul



Tue, 02 Mar 2004 05:14:29 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Reg Exp Search in Asm?

2. Is // a reg exp?

3. reg exp help needed

4. Reg Exp in awk

5. Why no reg exp in Forth?

6. Reg-exp library?

7. Newbie Generic Reg Exp Pattern Matching Question

8. Newbie: searching an English dictionary for a reg exp

9. Reg Exp. Problem

10. Reg Exp: Need advice concerning "greediness"

11. Expect reg-exp trouble

12. variable repetition in formatted i/o

 

 
Powered by phpBB® Forum Software