Having to use regex for the first time and although I almost achieved what I require I do not seems to be able to combine into a single statement.
I have a string of words where I wish to replace \n if it is not preceded by a dot OR not preceded by a dot space.
I can run either of these two statements to achieve the required result. However, if I either run them one after another or try to combine them into a single regex, it does not work.
//replaces \n if not preceded by dot space
xx = xx.replaceAll("(.+)(?<!\\. )\n", "$1 ");
//replaces \n if not preceded by dot
xx = xx.replaceAll("(.+)(?<!\\.)\n", "$1 ");
//one of my attempts to combine into a single statement
xx = xx.replaceAll("(.+)(?<!\\. )\n|(?<!\\.)\n", "$1 ");
Example of String I’m trying to fix.
BEFORE
This is some text which may\n have a newline character to break the line\n but I only want to remove it if it's not preceded with a full.\n or it's not preceded with a full stop and a space. \n
AFTER
This is some text which may have a newline character to break the line but I only want to remove it if it's not preceded with a full.\n or it's not preceded with a full stop and a space. \n
I think I’m close, but being new to regex, I am getting more confused the more I read.
It’s easier than you think:
Explanation:
So you don’t need to match
(.+)in the first place, only to replace it with itself afterwards. Incidentally, here’s what tripped you up:is logically grouped as
so the
(.+)is only matched if there is no space after the dot.