Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正则表达式找不到字符串_Java_Regex - Fatal编程技术网

Java 正则表达式找不到字符串

Java 正则表达式找不到字符串,java,regex,Java,Regex,我对此代码有问题: 由于某些原因,它总是与代码不匹配 for (int i = 0; i < pluginList.size(); i++) { System.out.println(""); String findMe = "plugins".concat(FILE_SEPARATOR).concat(pluginList.get(i)); Pattern pattern = Pattern.compile("("+name.getPath()+")(.*)")

我对此代码有问题: 由于某些原因,它总是与代码不匹配

for (int i = 0; i < pluginList.size(); i++) {
    System.out.println("");
    String findMe = "plugins".concat(FILE_SEPARATOR).concat(pluginList.get(i));

    Pattern pattern = Pattern.compile("("+name.getPath()+")(.*)");
    Matcher matcher = pattern.matcher(findMe);

    // Check if the current plugin matches the string.
    if (matcher.find()) {
        return !pluginListMode;
    }
}
for(inti=0;i
现在我们只能猜测,因为我们不知道
name.getPath()
可能会返回什么

我怀疑它失败了,因为该字符串可能包含在正则表达式中具有特殊意义的字符。请再试一次

Pattern pattern = Pattern.compile("("+Pattern.quote(name.getPath())+")(.*)");
然后看看会发生什么


另外,
(.*)
部分(甚至是
name.getPath()
result周围的括号)似乎根本不重要,因为您没有对匹配结果本身做任何处理。在这一点上,问题是为什么要首先使用正则表达式。

现在我们只能猜测,因为我们不知道
name.getPath()
可能返回什么

我怀疑它失败了,因为该字符串可能包含在正则表达式中具有特殊意义的字符。请再试一次

Pattern pattern = Pattern.compile("("+Pattern.quote(name.getPath())+")(.*)");
然后看看会发生什么


另外,
(.*)
部分(甚至是
name.getPath()
result周围的括号)似乎根本不重要,因为您没有对匹配结果本身做任何处理。在这一点上,问题是为什么您首先要使用正则表达式。

您真正需要的是

return ("plugins"+FILE_SEPARATOR+pluginName).indexOf(name.getPath()) != -1;
但是您的代码也没有意义,因为对于-loop来说,-loop无法进入第二次迭代——它无条件返回。所以你更可能需要这样的东西:

for (String pluginName : pluginList)
  if (("plugins"+FILE_SEPARATOR+pluginName).indexOf(name.getPath()) != -1)
    return false;
return true;

你真正需要的是

return ("plugins"+FILE_SEPARATOR+pluginName).indexOf(name.getPath()) != -1;
但是您的代码也没有意义,因为对于-loop来说,-loop无法进入第二次迭代——它无条件返回。所以你更可能需要这样的东西:

for (String pluginName : pluginList)
  if (("plugins"+FILE_SEPARATOR+pluginName).indexOf(name.getPath()) != -1)
    return false;
return true;

请提供一个简短但完整的程序来说明问题。哦,为了可读性,将return语句部分改为
return!插件式请提供一个简短但完整的程序来演示问题。哦,为了可读性,将return语句部分改为
return!插件式我无法使正则表达式工作。我已经试过你的建议了,但似乎没有任何效果。我使用了下面的选项来解决这个问题,谢谢。我无法让正则表达式工作。我已经试过你的建议了,但似乎没有任何效果。我使用了下面的选项来解决这个问题,谢谢。