使用file.seperator分割Groovy [英] Groovy split using file.seperator

查看:74
本文介绍了使用file.seperator分割Groovy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到如下错误

Groovy script throws an exception of type class 
java.util.regex.PatternSyntaxException with message = 
Unexpected internal error near index 1
\
 ^

来自Split语句,如下所示:

from the Split statement as follows:

 String strClassPath = System.getProperty("java.class.path");
 String[] path = strClassPath.split(System.getProperty("file.separator"));

我应该如何使它在UNIX和Windows系统上正常工作(这就是为什么我使用"file.separator")

How should I make this work correctly for both UNIX and Windows systems (that's why I'm using "file.separator")

非常感谢

推荐答案

这将调用Java的split(String regexp).因此,您的输入必须是正则表达式(或必须用引号引起来):

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']

对于regexp/split来说太多了.如果您走这条路,最好使用Path.例如

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']

这篇关于使用file.seperator分割Groovy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆