Match array to string in java

I am reading a file using bufferedreader, so let's say that I have

line = br.readLine(); 

I want to check if this string contains one of many possible strings (which I have in the array). I would like to write something like:

 while (!line.matches(stringArray) { // not sure how to write this conditional do something here; br.readLine(); } 

I'm new to programming and Java, am I doing this right?

+4
source share
3 answers

Copy all the values ​​into Set<String> , and then use contains() :

 Set<String> set = new HashSet<String> (Arrays.asList (stringArray)); while (!set.contains(line)) { ... } 

[EDIT] If you want to know if a part of a string contains a string from a set, you need to loop over the set. Replace set.contains(line) with the call:

 public boolean matches(Set<String> set, String line) { for (String check: set) { if (line.contains(check)) return true; } return false; } 

Adjust the check accordingly when you use regexp or a more sophisticated method for matching.

[EDIT2] The third option is to combine the elements in an array in a huge regular expression with | :

 Pattern p = Pattern.compile("str1|str2|str3"); while (!p.matcher(line).find()) { // or matches for a whole-string match ... } 

It can be cheaper if you have many elements in the array, as regexp code optimizes the matching process.

+3
source

It depends on what stringArray . If this is a Collection , then great. If it is a true array, you should make it a Collection . The Collection interface has a contains() method that determines whether a given Object in a Collection .

An easy way to turn an array into a Collection :

 String tokens[] = { ... } List<String> list = Arrays.asList(tokens); 

The problem with List is that the search for roads (technically linear or O(n) ). It is best to use a Set that is disordered but has an almost constant ( O(1) ) search. You can build one like this:

From Collection :

 Set<String> set = new HashSet<String>(stringList); 

From the array:

 Set<String> set = new HashSet<String>(Arrays.asList(stringArray)); 

and then set.contains(line) will be a cheap operation.

Edit: Well, I think your question is not clear. You want to see if a string contains any of the words in the array. Then you want something like this:

 BufferedReader in = null; Set<String> words = ... // construct this as per above try { in = ... while ((String line = in.readLine()) != null) { for (String word : words) { if (line.contains(word)) [ // do whatever } } } } catch (Exception e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (Exception e) { } } } 

This is a pretty crude test, which is used surprisingly open and tends to produce annoying false positives on words like "scrap." For a more complex solution, you probably have to use a regular expression and look for word boundaries:

 Pattern p = Pattern.compile("(?<=\\b)" + word + "(?=\b)"); Matcher m = p.matcher(line); if (m.find() { // word found } 

You probably want to do this more efficiently (for example, not compiling the template with each line), but here is the main tool to use.

+1
source

Using the String.matches(regex) function, how about creating a regular expression that matches any of the lines in the string array? Sort of

 String regex = "*("; for(int i; i < array.length-1; ++i) regex += array[i] + "|"; regex += array[array.length] + ")*"; while( line.matches(regex) ) { //. . . } 
0
source

All Articles