From: Brian Candler Date: 2003-05-13T07:54:02+09:00 Subject: Re: Regexp: why does (re)* return only last repetition? 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 \ ] which is not a valid RE, because it has no terminating close-bracket. \] means "match a literal ]", so you have [ start of character class a first character in range - range separator \] last character in range but no 'end of character class' It's the same with /.../ delimiters: irb(main):001:0> /[a-z\]/ SyntaxError: compile error (irb):1: premature end of regular expression: /[a-z\]/ from (irb):1 > Change the pattern to "[\\a-z]" and the error goes away. Yes, that's [ \ a - z ] I guess '\a' is treated as just 'a' in a character class. It's not 'match start of string' anyway, as that doesn't make sense in a character class... Regards, Brian.