如何使用java查找行中的给定字符串

如何使用java查找行中的给定字符串,java,pattern-matching,Java,Pattern Matching,我想在一行中找到一个给定的sting。为此,我编写了代码。我使用Pattern类来查找给定字符串在一行中是否可用。它是给定的行,那些不是给定的行。我想要具有给定字符串的行。请检查我的代码 代码: 您可以通过以下方式进行尝试: String test="demo string"; System.out.println(test.indexOf("demo")); 输出: 注意:如果indexOf找到字符串,它将返回第一个字符的位置,否则返回-1。或者,也可以使用正则表达式搜索复杂字符串 使用正则

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

代码:


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

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

注意:如果indexOf找到字符串,它将返回第一个字符的位置,否则返回-1。或者,也可以使用正则表达式搜索复杂字符串

使用正则表达式:

输出:

您必须使用如下模式和匹配器类:

Pattern p = Pattern.compile(myRegex);
Matcher m = p.matcher(myString);
while(m.find()){
//print m.group();
}
或者你应该使用

然后将返回true或false

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

System.out.println(test.toLowerCase().contains(new String("demo").toLowerCase()));
contains区分大小写

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

编辑


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

由于您的输入中有换行符,因此还需要添加?s以启用DOTALL标志,这将导致点也与换行符匹配:

boolean b = Pattern.matches("(?s).*\\b"+searchStr+"\\b.*", line);

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

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

我希望这会对你有所帮助

我下面的答案对你有用吗

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

    }


}

我想找出准确的字符串,而不是子字符串。你可以相应地搜索词,无论如何,请查看我的更新答案。它还显示不存在给定搜索字符串的行。与我的代码相同。它显示不匹配。但文本具有。我已发布我的输入代码,如上所述..*?有点混乱-just.*将匹配相同的输入。它显示非给定的字符串语句。好的,请尝试在此处使用您的大脑。再往前走一点并不难。我添加了一个编辑,告诉你如何打印出找到的内容。请运行上面的代码。它显示了什么。sample.txt文件包含文本。我用字符串变量文本初始化该文本,以了解文件中的内容。请检查我的任务。谢谢大家。没关系。但是,它也可以找到给定字符串的子字符串。例如,在我输入的第6行。我只想找到确切的字符串。谢谢回复。您好,我已经更新了我的代码,它会打印EXACT搜索字符串。这是你想要的吗?好的,很好。如果搜索字符串来自用户输入的另一个文本字段。我如何给出这种情况。例如searchStr=textField.getText;这也表明第六行是正确的。但是,第六行没有准确的。嗯,好的,我想我之前误解了你的评论。我又更新了。它和你想要的相符吗?
String test="demo string";
System.out.println(test.contains("demo"));
System.out.println(test.toLowerCase().contains(new String("demo").toLowerCase()));
String str = new String("demo");
if (test.toLowerCase().contains(str.toLowerCase())) {
    System.out.println(str);
}
boolean b = Pattern.matches("(?s).*\\b"+searchStr+"\\b.*", line);
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");
      }
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");
         }

    }


}