2

I am using a regular expression for finding string in between two strings

Code:

        Pattern pattern = Pattern.compile("EMAIL_BODY_XML_START_NODE"+"(.*)(\\n+)(.*)"+"EMAIL_BODY_XML_END_NODE");
        Matcher matcher = pattern.matcher(part);
        if (matcher.find()) {
                      ..........

It works fine for texts but when text contains special characters like newline it's break

1 Answer 1

3

You need to compile the pattern such that . matches line terminaters as well. To do this you need to use the DOTALL flag.

Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);

edit: Sorry, it's been a while since I've had this problem. You'll also have to change the middle regex from (.*)(\\n+)(.*) to (.*?). You need to lazy quantifier (*?) if you have multiple EMAIL_BODY_XML_START_NODE elements. Otherwise the regex will match the start of the first element with the end of the last element rather than having separate matches for each element. Though I'm guessing this is unlikely to be the case for you.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, MULTILINE does the opposite of what I thought it did. You're really after the DOTALL flag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.