我是Java的新手,一直在尝试写一些代码行,其中的要求是将正则表达式将保存在文件中,从文件中读取内容并将其保存在数组列表中,然后与一些字符串变量进行比较并找到匹配项。但是在此过程中,当我尝试执行其匹配的单个字母而不是整个单词时。下面是代码。



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);


}





sql.pattern包含

\\buser\\b \\border\\b

期望它不匹配任何内容,并显示一条消息说找不到匹配项,而是显示找到匹配项,并且m.group()打印出字母s作为输出,有人可以帮忙。

提前致谢。

最佳答案

这里的问题似乎是双斜线。

我不建议您在list.toString()方法中提供Pattern.compile,因为它还会插入'[',','和']'字符,这可能会使您的正则表达式混乱,而是可以参考以下代码:

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);

  }
}


同时保持/code/sql.pattern如下:

\buser\b
\border\b
\bfrom\b

10-05 20:35
查看更多