pescador_bob (5) [Avatar] Offline
#1
It seems groovy console either messes up, or the book is wrong about the code in section 3.5.3 (listing 3.6). Here is my output:

groovy> myFairStringy = 'The rain in Spain stays mainly in the plain!'
groovy> // words that end with 'ain': w*ain
groovy> BOUNDS = //
groovy> rhyme = /$BOUNDSw*ain$BOUNDS/
groovy> found = ''
groovy> myFairStringy.eachMatch(rhyme) { match -> //#1
groovy> found += match[0] + ' '
groovy> }
groovy> assert found == 'rain Spain plain '
groovy> found = ''
groovy> (myFairStringy =~ rhyme).each { match -> //#2
groovy> found += match + ' '
groovy> }
groovy> assert found == 'rain Spain plain '
groovy> cloze = myFairStringy.replaceAll(rhyme){ it-'ain'+'___' } //#3
groovy> assert cloze == 'The r___ in Sp___ stays mainly in the pl___!'

Exception thrown: java.lang.AssertionError: Expression: (found == rain Spain plain ). Values: found = r S p

java.lang.AssertionError: Expression: (found == rain Spain plain ). Values: found = r S p
at Script24.run(Script24:11)
pescador_bob (5) [Avatar] Offline
#2
Re: Code error in Listing 3.6 (regular expressions)
I should say, I'm using Groovy 1.6-beta-1
pescador_bob (5) [Avatar] Offline
#3
Re: Code error in Listing 3.6 (regular expressions)
So, I see the problem. The problem is with the code listed here. Specifically line 9 says:
found += match[0] + ' '

but is should be:

found += match + ' '

otherwise, it just returns the first letter of the match.