Java matches the whole word in String

I have an ArrayList<String> , which I repeat to find the correct index given by String. In principle, given the string, the program should search the list and find the index where the whole word matches. For instance:

 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 String AAA , I have to return index 2 (last). Therefore, I cannot use the contains() method, as that would give me an index of 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 inside the loop. I'm not sure what I'm doing wrong.

Note. This should also work if I looked for AAA_0921 , full name AAA_09212017.txt or any part of a string unique to it.

+5
source share
1 answer

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

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

Here (?<=_|\b) positive lookbehind requires that a word boundary or underscore appear before str , and a positive look -head (?=_|\b) requires an underscore or word to appear immediately after str .

Watch this demo version of regex .

If your word may have special characters inside, you can use a more straightforward word boundary:

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

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

Note that in the second example there is a search quote, so even AA.A_str.txt can be matched well with AA.A

Watch another regex demo

+5
source

All Articles