Reputation: 1975
I have 2 lines (separated by 0, 1 or many crlf), the value of the first line is always "line1", and thanks to this value I try to capture value of the second line.
Everything is working well when there is only one crlf between the 2 lines, but when there is more than one the regex is also capturing the crlf.
This is my regex :
(?s)line1\r\n(.*?) ok
Basically I was thinking that this regex will work :
(?s)line1\r\n*(.*?) ok
But it's not, so how can i possibly indicate with the quantifier *, that there can be 0,1 or many crlf.
Thanks in advance for your help
Upvotes: 0
Views: 85
Reputation: 626794
Regarding
how can i possibly indicate with the quantifier *, that there can be 0,1 or many crlf.
You might want to match any newline symbol sequences with [\r\n]*
. This will match \r\n
, \n
, \r
any number of times. Your regex can look like (?s)line1[\r\n]*(.*?)
, then.
Upvotes: 0
Reputation: 69339
You were nearly there with your pattern. You don't actually need to enable DOTALL
with (?s)
. This pattern should suffice:
line1(?:\r\n)*(.*)
This matches line1
followed by zero or more \r\n
, then your second line.
String example = "line1\r\n\r\n\r\nline2";
Pattern pattern = Pattern.compile("line1(?:\r\n)+(.*)");
Matcher m = pattern.matcher(example);
if (m.find()) {
System.out.println(m.group(1));
}
Prints: line2
.
Upvotes: 2
Reputation: 1205
I would have try like this
(?s)line1(\r\n)*(.*)
You put * on \n only so new \r\n weren't matching
Upvotes: 0