问题
I'm getting an error as follows
Groovy script throws an exception of type class
java.util.regex.PatternSyntaxException with message =
Unexpected internal error near index 1
\
^
from the Split statement as follows:
String strClassPath = System.getProperty("java.class.path");
String[] path = strClassPath.split(System.getProperty("file.separator"));
How should I make this work correctly for both UNIX and Windows systems (that's why I'm using "file.separator")
Many thanks in advance
回答1:
This calls java's split(String regexp)
. So your input must be a regexp (or must be quoted):
import java.util.regex.Pattern
def cp = {path, sep ->
path.split(Pattern.quote(sep))
}
assert cp('C:\\window\\something\\groovy.jar', '\\') == ['C:', 'window', 'something', 'groovy.jar']
assert cp('/usr/local/share/groovy.jar', '/') == ['', 'usr', 'local', 'share', 'groovy.jar']
So much for the regexp/split. If you are after the path, you might be better off using Path
. e.g.
assert new File('/usr/local/share/groovy.jar').toPath().collect()*.toString() == ['usr', 'local', 'share', 'groovy.jar']
来源:https://stackoverflow.com/questions/27257759/groovy-split-using-file-seperator