Q:

Regex patter in Java matching single letter instead of complete word.

I am new to java and been trying to write some line of code where the requirement is something regex patter will be saved in file, read the content from file and save it array list then compare with some string variable and find the match.  But in this process when am trying to do its matching single letter instead of the whole word.  below is the code . 

import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.regex.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class RegexMatches { 

   public void findfile( String path ){
      File f = new File(path);
      if(f.exists() && !f.isDirectory()) { 
        System.out.println("file found.....!!!!");
        if(f.length() == 0 ){
          System.out.println("file is empty......!!!!");

}}
      else {
         System.out.println("file missing");
      }

}

    public void readfilecontent(String path, String sql){
     try{Scanner s = new Scanner(new File(path));
      ArrayList<String> list = new ArrayList<String>();
      while (s.hasNextLine()){
      list.add(s.nextLine());
      }
        s.close();
        System.out.println(list);
        
        Pattern p = Pattern.compile(list.toString(),Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(sql);
        if (m.find()){
          System.out.println("match found");
          System.out.println(m.group());
      }  
        else {System.out.println("match not found"); }

      }
        catch (FileNotFoundException ex){}
       
    }

public static void main( String args[] ) {

 String path = "/code/sql.pattern";
 String sql = "select * from  schema.test";
  RegexMatches regex = new RegexMatches();
  regex.findfile(path);
  regex.readfilecontent(path,sql);
  

}

the sql.pattern contains 
\\buser\\b 
 \\border\\b 
Am expecting that it shouldn't match anything and print message saying match not found instead it says match found and m.group() prints letter s as output could anyone please help. 
Thanks in advance. 

A:

The problem here seems to be the double slash.
I would not recommend you to provide list.toString() in Pattern.compile method  because it also inserts '[', ',' and ']' character which can mess up with you regex, instead you can refer below code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches {

  public void findfile(String path) {
    File f = new File(path);
    if (f.exists() && !f.isDirectory()) {
      System.out.println("file found.....!!!!");
      if (f.length() == 0) {
        System.out.println("file is empty......!!!!");

      }
    } else {
      System.out.println("file missing");
    }

  }

  public void readfilecontent(String path, String sql) {
    try {
      Scanner s = new Scanner(new File(path));
      ArrayList<String> list = new ArrayList<String>();
      while (s.hasNextLine()) {
        list.add(s.nextLine());
      }
      s.close();
      System.out.println(list);

      list.stream().forEach(regex -> {
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(sql);
        if (m.find()) {
          System.out.println("match found for regex " + regex );
          System.out.println("matched substring: "+ m.group());
        } else {
          System.out.println("match not found for regex " + regex);
        }
      });

    } catch (FileNotFoundException ex) {
      ex.printStackTrace();
    }

  }

  public static void main(String args[]) {

    String path = "/code/sql.pattern";
    String sql = "select * from schema.test";
    RegexMatches regex = new RegexMatches();
    regex.findfile(path);
    regex.readfilecontent(path, sql);

  }
}

while keeping /code/sql.pattern as below:
\buser\b
\border\b
\bfrom\b

