这是我在Groovy中尝试做的事情:

def url = "\\data\\aaa\\Service\\111"
def d = url.find("\\data\\aaa\\Service")

但是dnull,根据文档,这意味着它找不到匹配项。

我已经在没有斜杠的情况下进行了测试,并且效果很好,所以我知道问题与斜杠有关。

我如何才能在\\data\\aaa\\Service中找到\\data\\aaa\\Service\\111

编辑:

这是我的问题,到目前为止尝试解决此问题:
  • url/data/cpp/Service/111的形式出现
  • folder.path返回\data\cpp\Service\111\222

  • 所以我试图将URL中的"/"转换为"\",这使得url = "\data\cpp\Service\111"
    然后,我需要从返回的任何url中删除folder.path部分

    以下是这种努力的体现:
    def getList(String url) {
        def list = []
        def dir = new File(url)
        url= url.replaceAll("/", "\\\\")
        def d = url.find("\\\\data\\\\aaa\\\\Service")
        dir.eachDirRecurse
            { folder ->
                list << folder.path.replaceAll(url,'')
            }
        return list
    }
    

    但是没有什么可以替代的,因为它无法在url中找到folder.path

    最佳答案

    您需要逃脱斜线。以下可能会起作用:
    def d = url.find("\\\\data\\\\aaa\\\\Service")

    关于regex - 如何找到文件路径字符串的一部分?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17621362/

    10-11 22:26
    查看更多