有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

模式匹配如何使用java查找行中的给定字符串

我想在一条线上找到一个给定的刺。为此,我编写了代码。我使用Pattern类来查找给定字符串在一行中是否可用。它给出的是行,那些不是给定的字符串。我想要那些有给定字符串的行。请查一下我的密码

代码:

 public class StringMatch {
  public static void main(String args[]) throws FileNotFoundException, IOException{
    String text="dsfhgs the dgfre fh" +
                "hjfgeh fhg hghjr" +
                "fhvhjf the vgh fhjghf" +
                "fbvhjfd  fhvfg fjvhf" +
                "the hfdvhjf vhf  fhdvjfd" +
                "hfvbhd thehjdvhj hdfvhdf";
    String pattern = ".*?\\the\\b.*?";
    String f="C:\\TextFiles\\sample.txt";
    String line;
    BufferedReader br = new BufferedReader(new FileReader(f));
    while(br.readLine()!=null) {
        line=br.readLine();
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(line);
        if (m.find( )) {
            System.out.println("Found value: " + m.group(0) );
            System.out.println("Found value: " + m.group(1) );
            System.out.println("Found value: " + m.group(2) );
        } else {
            System.out.println("NO MATCH");
    }
    }
 }
}

共 (6) 个答案

  1. # 1 楼答案

    您可以通过以下方式进行尝试:

    String test="demo string";
    System.out.println(test.indexOf("demo"));
    

    输出:

    0
    

    注意:indexOf()如果找到字符串,将返回第一个字符的位置,否则返回-1。或者,也可以使用Regular Expressions来搜索复杂字符串

    Using Regular Expressions :

        Pattern p = Pattern.compile("[0-9]");
        Matcher m = p.matcher("23asd56");
        while(m.find()){
            System.out.println(m.group());
        }
    

    输出:

    2
    3
    5
    6
    
  2. # 2 楼答案

    或者你应该使用String Contains

    String test="demo string";
    System.out.println(test.contains("demo"));
    

    然后返回true或false

    如果你不在乎这个案子,你就不得不这么做

    System.out.println(test.toLowerCase().contains(new String("demo").toLowerCase()));
    

    contains区分大小写

    它不会做任何华丽的正则表达式,尽管只是一个简单的比较检查

    编辑

    String str = new String("demo");
    if (test.toLowerCase().contains(str.toLowerCase())) {
        System.out.println(str);
    }
    
  3. # 3 楼答案

    这里的问题是(与大多数其他语言不同)在java中,matches()必须匹配整个字符串才能为true,因此在模式的每一端添加".*"

    因为输入中有换行符,所以还需要添加(?s)来打开DOTALL标志,这会导致点也匹配换行符:

    boolean b = Pattern.matches("(?s).*\\b"+searchStr+"\\b.*", line);
    
  4. # 4 楼答案

    只需使用以下简单模式即可!!!:

    String pattern = ".*?\\the\\b.*?";
    
          // Create a Pattern object
          Pattern r = Pattern.compile(pattern);
    
          // Now create matcher object.
          Matcher m = r.matcher(line);
          if (m.find( )) {
             System.out.println("Found value: " + m.group(0) );
             System.out.println("Found value: " + m.group(1) );
             System.out.println("Found value: " + m.group(2) );
          } else {
             System.out.println("NO MATCH");
          }
    

    我希望这对你有帮助

  5. # 5 楼答案

    您必须使用以下PatternMatcher类:

    Pattern p = Pattern.compile(myRegex);
    Matcher m = p.matcher(myString);
    while(m.find()){
    //print m.group();
    }
    
  6. # 6 楼答案

    我下面的答案对你有用吗

    public static void main (String [] args){
        String given = "dsfhgs the dgfre fh \n" +
                "hjfgeh fhg hghjr \n" +
                "fhvhjf the vgh fhjghf \n" +
                "fbvhjfd  fhvfg fjvhf \n" +
                "the hfdvhjf vhf  fhdvjfd \n" +
                "hfvbhd thehjdvhj hdfvhdf." ;
        String[] lines = given.split("\n");
        String searchStr = "the";       
         for(String line : lines)
         {
    
             String[] wordOfLine = line.split(" ");
             boolean match = false;
             for(String word : wordOfLine)
             {
                    if (searchStr.equals(word)) 
                    {
                        match = true;
                    } 
             }
    
             if(match)
             {
                 System.out.println("Found value: " + searchStr);
             }
             else 
             {
                 System.out.println("NO MATCH");
             }
    
        }
    
    
    }