From: Brian Candler Date: 2003-05-13T16:10:36+09:00 Subject: Re: Regexp: why does (re)* return only last repetition? On Tue, May 13, 2003 at 12:02:06PM +0900, Austin Ziegler wrote: > On Tue, 13 May 2003 07:54:02 +0900, Brian Candler wrote: > > On Tue, May 13, 2003 at 07:29:24AM +0900, Austin Ziegler wrote: > >> You changed the pattern. > >> > >> 1 | pat = "[a-z\\]" > >> 2 | str = "abcd\\efgh" > >> 3 | index = /#{pat}/ =~ str > >> 4 | p index > >> > >> line 3 results in: > >> RegexpError: premature end of regular expression: /[a-z\]/ > > Double-quoted strings interpolate backslashes: so in your example > > pat contains: > > [a-z\] > > No: > irb(main):001:0> "[a-z\\]" > => "[a-z\\]" Now try: a = "[a-z\\]" p a.length # => 6 a.each_byte { |c| puts "%c" % c } # => [ a - z \ ] irb is deceiving you, because 'inspect' outputs strings in a way which can be re-input as strings into Ruby. A single backslash is printed as two backslashes. > It's rather irrelevant anyway, as the *exact same behaviour* happens > with single-quoted strings (change line 1 to '' instead of ""). And single-quoted strings have the same issue it turns out: a = '\\' p a.length # => 1 I am not sure why that should be, since '\n'.length is 2. I think you may have uncovered a bug here. > No: > irb(main):009:0> /[\a]/ =~ "abc\a" > => 3 "\a" in a double-quoted string is a 'BEL' (a for Audible), ASCII code 7 irb(main):001:0> "\a"[0] => 7 Brian.