I'm a newbie on Groovy and have a question about replaceFirst
with closure.
The groovy-jdk API doc gives me examples of...
assert "hellO world" == "hello world".replaceFirst("(o)") { it[0].toUpperCase() } // first match
assert "hellO wOrld" == "hello world".replaceAll("(o)") { it[0].toUpperCase() } // all matches
assert '1-FISH, two fish' == "one fish, two fish".replaceFirst(/([a-z]{3})\s([a-z]{4})/) { [one:1, two:2][it[1]] + '-' + it[2].toUpperCase() }
assert '1-FISH, 2-FISH' == "one fish, two fish".replaceAll(/([a-z]{3})\s([a-z]{4})/) { [one:1, two:2][it[1]] + '-' + it[2].toUpperCase() }
The first two examples are quite straightforward, but I can't understand the remaining ones.
First, what does [one:1, two:2]
mean?
I even don't know the name of it to search.
Second, why is there a list of "it"? The doc says replaceFirst()
Replaces the first occurrence of a captured group by the result of a closure call on that text.
Doesn't "it" refer to "the first occurrence of a captured group"?
I would appreciate any tips and comments!