Reputation: 6910
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
Upvotes: 2
Views: 539
Reputation: 40683
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.
Upvotes: 3