0

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.

Adarsh H D Dev
  • 588
  • 7
  • 29
  • 1
    Your pattern is defined with `list.toString()`, I doubt you need that. – Wiktor Stribiżew May 08 '18 at 09:35
  • my pattern are saved in list, if i directly pass list like . this `Pattern.compile(list)` i got type mismatch error hence i put that there. Am not sure whether that is the best way or not, if you have alternative please suggest i will try it out. And more over my pattern is saved in Arraylist object without passing that for compile how will compare that . ? – Adarsh H D Dev May 08 '18 at 09:39
  • Print the result of `list.toString()` and you will see what "pattern" you are using. You will need to get either the first value of the `String` or concatenate those as you need, but it will not be enough to "stringify" the list. – AxelH May 08 '18 at 09:51
  • Look, you can only provide a string to `Pattern.compile` as the first argument. If you have multiple literal strings, you might want to use `String.join("|", list)`, or - if those items consist of only alphanumeric/word characters and you plan to search for whole words - `"\\b(?:" + String.join("|", list) + ")\\b"`. Since it makes sense to escape special chars in the literal strings, I'd advise escaping the search words [like at the bottom of this answer](https://stackoverflow.com/a/43841961/3832970). – Wiktor Stribiżew May 08 '18 at 09:52
  • I modify the code but its not helping now its not even matching the single letter below is the changes System.out.println(String.join("|", list.toString())); Pattern p = Pattern.compile("\\b(?:" + String.join("|", list.toString()) + ")\\b",Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(sql); – Adarsh H D Dev May 08 '18 at 10:08

1 Answers1

1

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
Gagan Chouhan
  • 312
  • 1
  • 6