简体   繁体   中英

Need a regular expression to match multiline String

I need a regular expression for the multiline string as below

Customer:
            VA000347
            VA000347
            Ashu Corp
            Others
            Enterprise

                Mumbai
                5
                Mumbai
                Maharashtra
                232323
                India

        :customer

I want to extract the later part ie "customer" from the multiline string using regular expression in java and I can match the first part(Customer) literally.

Any help is greatly appreciated.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM