I have this type of text from a tag:
RP/HDQ1S/HDQ1SJFDEAK/C8HJ/2024837 20APR23/0339Z GLS123 DATA 001 OS/AF1638 J 16APR 7 CDGHAJ LK1 1250 1420/LK *1A/E* 002 OS/AF1439 Q 20APR 4 HAJCDG LK1 0600 0735/LK *1A/E* 003 OS/AF9466 Q 20APR 4 CDGRNS LK1 1235 1340/LK *1A/E* 004 RF-HDQRM1S 081330 DK07E856C11618 300AB28DB011CD//HDQ1SJF DEAK/C8HJ/20248373/LBG/1S/T/FR/EUR CR-HDQ RM 1S 08MAR133 0Z
I’m trying to merge the lines like that:
RP/HDQ1S/HDQ1SJFDEAK/C8HJ/2024837 20APR23/0339Z GLS123 DATA 000 OS/AF1638 J 16APR 7 CDGHAJ LK1 1250 1420/LK *1A/E* 000 OS/AF1439 Q 20APR 4 HAJCDG LK1 0600 0735/LK *1A/E* 000 OS/AF9466 Q 20APR 4 CDGRNS LK1 1235 1340/LK *1A/E* 000 RF-HDQRM1S 081330 DK07E856C11618 300AB28DB011CD//HDQ1SJFDEAK/C8HJ/20248373/LBG/1S/T/FR/EUR CR-HDQ RM 1S 08MAR1330Z
With the following expression, I managed to exclude the first line, containing "RP", but I’m only getting one match and I need to get two:
(^(?!RP).+)((n )|(n ))
I’m using it in JavaScript to replace like that:
text.textContent = text.textContent.replace(/^(?!RP)(.+)((n )|(n ))/gm, function (match, p1) { return p1 });
The goal is to merge every new line with the previous one if the new line start with 6 or 8 empty spaces, and to ignore the first line that starts with RP.
2
Answers
You’ll have to run it multiple times.
If a positive lookbehind is supported in your environment, you can assert that the previous line does not start with RP and then match a newline followed by 6 or 8 spaces which are not followed by a space:
Regex demo
You could also check if DATA is not after the match
Regex demo