Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: 1.0
-
Fix Version/s: 1.5.2
-
Component/s: groovy-jdk
-
Labels:None
-
Environment:all
-
Number of attachments :
Description
The current implementation of String.minus fails if the operand contains regular expression special characters. The current implementation looks like this:
/**
- Remove a part of a String
* - @param left a String
- @param value a String part to remove
- @return a String minus the part to be removed
*/
public static String minus(String left, Object value) { String text = toString(value); return left.replaceFirst(text, ""); }
I would suggest to escape all regex special characters in the "text" String or to choose a different implementation.
Examples of wrong or strange behaviour:
groovy> println "asd" - "a
c"
groovy> go
Caught: java.util.regex.PatternSyntaxException: Illegal control escape sequence near index 2
a\c
^
at gjdk.java.lang.String_GroovyReflector.invoke(Unknown Source)
at CommandLine.run(CommandLine.groovy:1)
groovy> println "should_not_match_but_matches"-"should_...match"
groovy> go
but_matches
groovy> println "asd"-"a
ud"
groovy> go
Caught: java.util.regex.PatternSyntaxException: Illegal Unicode escape sequence near index 4
a\ud
^
at gjdk.java.lang.String_GroovyReflector.invoke(Unknown Source)
at CommandLine.run(CommandLine.groovy:1)
hmm...some of my examples above are messed up, because Jira replaces my double-backslashes with newlines. So here they come again: