java - Java 中的正则表达式模式匹配单个字母而不是完整的单词。

标签 java regex

我是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 \\边框\\b

我希望它不匹配任何内容并打印消息说找不到匹配,而是说找到匹配并且 m.group() 打印字母 s 作为输出,任何人都可以帮忙。

提前致谢。

最佳答案

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

我不建议您在 Pattern.compile 方法中提供 list.toString() 因为它还会插入 '[', ' ,' 和 ']' 字符可能会扰乱你的正则表达式,你可以引用下面的代码:

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

关于java - Java 中的正则表达式模式匹配单个字母而不是完整的单词。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50230376/

相关文章:

java - JVM 是否使用 kotlinc 来实现 Kotlin?

具有不同匹配和替换条件的 Java String.replaceAll

javascript - 用派生字符串 JavaScript 替换字符串中的坐标

java - 提取查询参数的正则表达式因日期时间字符串而失败

c - 使用 PCRE 正则表达式的奇怪答案

java - JavaScript 和 Java 之间正则表达式模式的差异?

java - 单击链接时 Android WebView 打开 uTorrent 应用程序

java - 没有 Persistence.xml 的 EclipseLink 和 Spring 配置

Java:本地搜索 TSP 错误

java - 停止无限循环 Runnable 从 ThreadPool 运行