0

I am having issues with this code: For some reason, it always fails to match the code.

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;
    }
}
1
  • 1
    Please provide a short but complete program which demonstrates the problem. Oh, and for readability, change your return statement part to return !pluginListMode; Commented May 9, 2012 at 9:25

2 Answers 2

2

All you really need is

return ("plugins"+FILE_SEPARATOR+pluginName).indexOf(name.getPath()) != -1;

But your code also makes no sense due to the fact that there's no way for that for-loop to enter a second iteration -- it returns unconditionally. So more probably you need something like this:

for (String pluginName : pluginList)
  if (("plugins"+FILE_SEPARATOR+pluginName).indexOf(name.getPath()) != -1)
    return false;
return true;
Sign up to request clarification or add additional context in comments.

Comments

2

Right now we can only guess since we don't know what name.getPath() might return.

I suspect it fails because that string might contain characters that have special meaning inside regexes. Try it again with

Pattern pattern = Pattern.compile("("+Pattern.quote(name.getPath())+")(.*)");

and see what happens then.

Also the (.*) part (and even the parentheses around your name.getPath() result) don't appear to matter at all since you're not doing anything with the result of the match itself. At which point the question is why you're using a regex in the first place.

1 Comment

I was not able to get regular expressions working. I have tried what you suggested, but nothing seemed to work. I used the option below to resolve this, thanks anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.