【问题标题】:Regex patter in Java matching single letter instead of complete word.Java 中的正则表达式模式匹配单个字母而不是完整的单词。
【发布时间】:2018-05-08 19:55:36
【问题描述】:

我是 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(list) 我得到了类型不匹配错误,因此我把它放在那里。我不确定这是否是最好的方法,如果您有其他选择,请建议我尝试一下。而且我的模式更多地保存在 Arraylist 对象中,而没有将其传递给 compile 如何比较它。 ?
  • 打印list.toString()的结果,你会看到你正在使用什么“模式”。您将需要获取 String 的第一个值或根据需要连接它们,但这不足以“字符串化”列表。
  • 你看,你只能提供一个字符串给Pattern.compile作为第一个参数。如果您有多个文字字符串,您可能希望使用 String.join("|", list),或者 - 如果这些项目仅包含字母数字/单词字符并且您计划搜索整个单词 - "\\b(?:" + String.join("|", list) + ")\\b"。由于在文字字符串中转义特殊字符是有意义的,我建议转义搜索词like at the bottom of this answer
  • 我修改了代码但它现在没有帮助它甚至不匹配下面的单个字母是更改 System.out.println(String.join("|", list.toString()));模式 p = Pattern.compile("\\b(?:" + String.join("|", list.toString()) + ")\\b",Pattern.CASE_INSENSITIVE);匹配器 m = p.matcher(sql);

标签: java regex


【解决方案1】:

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

我不建议您在 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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2010-11-15
  • 2012-01-06
  • 2011-12-16
  • 2013-09-16
  • 2011-08-07
相关资源
最近更新 更多