RegEx in Java: how to deal with newline

后端 未结 6 1269
小蘑菇
小蘑菇 2020-12-01 05:17

I am currently trying to learn how to use regular expressions so please bear with my simple question. For example, say I have an input file containing a bunch of links separ

6条回答
  •  悲&欢浪女
    2020-12-01 05:47

    This version matches newlines that may be either Windows (\r\n) or Unix (\n)

    Pattern p = Pattern.compile("(www.*)((\r\n)|(\n))(.*Pig.*)");
    String s = "www.foo.com/Archives/monkeys.htm\n"
               + "Description of Monkey's website.\n"
               + "\r\n"
               + "www.foo.com/Archives/pigs.txt\r\n"
               + "Description of Pig's website.\n"
               + "\n"
               + "www.foo.com/Archives/kitty.txt\n"
               + "Description of Kitty's website.\n"
               + "\n"
               + "www.foo.com/Archives/apple.htm\n"
               + "Description of Apple's website.\n";
    Matcher m = p.matcher(s);
    if (m.find()) {
      System.out.println("found: "+m.group());
      System.out.println("website: "+m.group(1));
      System.out.println("description: "+m.group(5));
    }
    System.out.println("done");
    

提交回复
热议问题