Details
Description
String.replaceAll(String, Closure) is passing the stringified closure result directly to Matcher.appendReplacement.
The consequence is that the resulting string is interpreted as a "replacement string" which treats '$' and '\' specially so that group replacements can be done. For my purposes that definitely undesireable.
The whole reason I see for the closure API is so that those substitutions are done in the closure and that the closure expects it's result string to be substituted literally.
An example of the problem is like this:
groovy> println 'x123z45'.replaceAll(/([^z]*)(z)/, { all, m, d -> m })
x12345
groovy> println '$123z45'.replaceAll(/([^z]*)(z)/, { all, m, d -> m })
$1232345
The fix is that quoteReplacement needs to be called on the result before appendReplacement is called. This is similar to the problem we had with minus.
http://jira.codehaus.org/browse/GROOVY-1637
Hmmm, there they avoided the problem that quoteReplacement is JDK 1.5 by not using replaceAll. Looks like we need to implement RegexUtils.quoteReplacement after all.
Activity
| Field | Original Value | New Value |
|---|---|---|
| Assignee | Jim White [ jimwhite ] |
| Status | Open [ 1 ] | In Progress [ 3 ] |
| Description |
String.replaceAll(String, Closure) is passing the stringified closure result directly to Matcher.appendReplacement. The consequence is that the resulting string is interpreted as a "replacement string" which treats '$' and '\' specially so that group replacements can be done. For my purposes that definitely undesireable. The whole reason I see for the closure API is so that those substitutions are done in the closure and that the closure expects it's result string to be substituted literally. An example of the problem is like this: groovy> println 'x123z45'.replaceAll(/([^z]*)(z)/, { all, m, d -> m }) x12345 groovy> println '$123z45'.replaceAll(/([^z]*)(z)/, { all, m, d -> m }) $1232345 The fix is that quoteReplacement needs to be called on the result before appendReplacement is called. This is similar to the problem we had with minus. http://jira.codehaus.org/browse/GROOVY-1637 Hmmm, there they avoided the problem that quoteReplacement is JDK 1.5 by not using replaceAll. Looks like we need to implement RegexUtils.quoteReplacement after all. |
String.replaceAll(String, Closure) is passing the stringified closure result directly to Matcher.appendReplacement. The consequence is that the resulting string is interpreted as a "replacement string" which treats '$' and '\' specially so that group replacements can be done. For my purposes that definitely undesireable. The whole reason I see for the closure API is so that those substitutions are done in the closure and that the closure expects it's result string to be substituted literally. An example of the problem is like this: {code} groovy> println 'x123z45'.replaceAll(/([^z]*)(z)/, { all, m, d -> m }) x12345 groovy> println '$123z45'.replaceAll(/([^z]*)(z)/, { all, m, d -> m }) $1232345 {code} The fix is that quoteReplacement needs to be called on the result before appendReplacement is called. This is similar to the problem we had with minus. http://jira.codehaus.org/browse/GROOVY-1637 Hmmm, there they avoided the problem that quoteReplacement is JDK 1.5 by not using replaceAll. Looks like we need to implement RegexUtils.quoteReplacement after all. |
| Resolution | Fixed [ 1 ] | |
| Status | In Progress [ 3 ] | Resolved [ 5 ] |
| Status | Resolved [ 5 ] | Closed [ 6 ] |
Fix committed to trunk cs14758.