5

I have an ArrayList<String> which I iterate through to find the correct index given a String. Basically, given a String, the program should search through the list and find the index where the whole word matches. For example:

ArrayList<String> foo = new ArrayList<String>();
foo.add("AAAB_11232016.txt");
foo.add("BBB_12252016.txt");
foo.add("AAA_09212017.txt");

So if I give the String AAA, I should get back index 2 (the last one). So I can't use the contains() method as that would give me back index 0.

I tried with this code:

String str = "AAA";
String pattern = "\\b" + str + "\\b";
Pattern p = Pattern.compile(pattern);

for(int i = 0; i < foo.size(); i++) {
    // Check each entry of list to find the correct value
    Matcher match = p.matcher(foo.get(i));

    if(match.find() == true) {
        return i;
    }
}

Unfortunately, this code never reaches the if statement inside the loop. I'm not sure what I'm doing wrong.

Note: This should also work if I searched for AAA_0921, the full name AAA_09212017.txt, or any part of the String that is unique to it.

4
  • 2
    _ is considered a word character..so it wont act as word boundary..
    – rock321987
    Commented Jul 6, 2016 at 16:49
  • Ooh, I didn't know that. I'll look into another expression.
    – syy
    Commented Jul 6, 2016 at 16:52
  • Don't you mean return i;? Commented Jul 6, 2016 at 16:53
  • I did, thanks for noticing!
    – syy
    Commented Jul 6, 2016 at 16:55

1 Answer 1

7

Since word boundary does not match between a word char and underscore you need

String pattern = "(?<=_|\\b)" + str + "(?=_|\\b)";

Here, (?<=_|\b) positive lookbehind requires a word boundary or an underscore to appear before the str, and the (?=_|\b) positive lookahead requires an underscore or a word boundary to appear right after the str.

See this regex demo.

If your word may have special chars inside, you might want to use a more straight-forward word boundary:

"(?<![^\\W_])" + Pattern.quote(str) + "(?![^\\W_])"

Here, the negative lookbehind (?<![^\\W_]) fails the match if there is a word character except an underscore ([^...] is a negated character class that matches any character other than the characters, ranges, etc. defined inside this class, thus, it matches all characters other than a non-word char \W and a _), and the (?![^\W_]) negative lookahead fails the match if there is a word char except the underscore after the str.

Note that the second example has a quoted search string, so that even AA.A_str.txt could be matched well with AA.A.

See another regex demo

8
  • Thank you very much! This made it work. Can you explain the different parts of the expression? I'm fairly new to regex. if not, that's alright, I can look it up.
    – syy
    Commented Jul 6, 2016 at 16:57
  • 1
    @Flow One great tool is regex101.com. If you look in the top right panel, it explains the regex you give it. Granted, it doesn't support Java regex, but it is usually close enough to PCRE. Commented Jul 6, 2016 at 16:58
  • 2
    I am fighting with my son for the laptop in the train compartment, and I will try of course :) Commented Jul 6, 2016 at 16:58
  • Haha, I really appreciate it! @4castle Awesome, this will help greatly!
    – syy
    Commented Jul 6, 2016 at 17:01
  • Does this support other languages as well? Commented Apr 23, 2018 at 13:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.