On Mon, 12 May 2003 23:51:44 +0900, Brian Candler wrote:
> On Mon, May 12, 2003 at 10:18:00PM +0900, Austin Ziegler wrote:
>> On Mon, 12 May 2003 17:39:19 +0900, Robert Klemme wrote:
>>> Clifford Heath:
>>>> Robert Klemme wrote:
>>>>> Clifford Heath:
>>>>>> re = Regexp.new('[a-z\\]')
>>>>> Why not simply do
>>>>> re = /[a-z\\]/
>>>> Because it was part of a larger extended re.
>>> But you can still use re = /.../ instead of re = Regexp.new '...'
>> Again, if and only if one is specifying the regular expression
>> statically.
> It doesn't have to be static:
> 
> pat = "ana"
> str = "banana"
> index = /#{pat}/ =~ str
> p index         # >> 1

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\]/

Change the pattern to "[\\a-z]" and the error goes away.

When building a pattern that includes a character class specifier,
it needs to be specified carefully or Regexp.new(pat) and /#{pat}/
won't work.

-austin
-- Austin Ziegler, austin / halostatue.ca on 2003.05.12 at 18:23:38