I have a string like this:
this is my text
more text
more text
text I want
is below
I just want the text below the double line break and not the stuff before.
Here is what I thought should work:
myString.replaceFirst(".+?(\n\n)","");
However it does not work. Any help would be greatly appreciated
You should use the below regex for your purpose: –
Because, you want to match anything including the
newlinecharacter before it encounters two newline characters back to back.Note that
dot(.)does not matches anewline, so it would stop matching on encountering thefirst newline character.If you want your
dot(.)to match newline, you can usePattern.DOTALL, which in case ofstr.replaceFirst, is achieved by using(?s)expression.From the documentation of
Pattern.DOTALL: –