需要一个正则表达式来匹配多行字符串

时间:2013-04-26 05:34:28

标签: java regex

我需要多行字符串的正则表达式,如下所示

Customer:
            VA000347
            VA000347
            Ashu Corp
            Others
            Enterprise

                Mumbai
                5
                Mumbai
                Maharashtra
                232323
                India

        :customer

我想在java中使用正则表达式从多行字符串中提取后面的部分,即“customer”,我可以按字面意思匹配第一部分(Customer)。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

    String text = "Customer:\n"
            + "            VA000347\n"
            + "            VA000347\n"
            + "            Ashu Corp\n"
            + "            Others\n"
            + "            Enterprise\n"
            + "\n"
            + "                Mumbai\n"
            + "                5\n"
            + "                Mumbai\n"
            + "                Maharashtra\n"
            + "                232323\n"
            + "                India\n"
            + "\n"
            + "        :customer";
    Pattern pattern = Pattern.compile("(?<=Customer:\n)(?s)(.*)(?=:customer)");
    Matcher matcher = pattern.matcher(text);
    matcher.find();
    System.out.println(matcher.group(1));