1

I have the following string:

 MISSILE 010102 FUZES AGAINST  2       RNG=      0.0 T=  82.846
         TERMINAL POSITION: X=  -75933.67 Y=   -4469.98 Z=   25000.09
         LAUNCH POSITION: X= -127017.19 Y=  -12934.89 Z=   25506.47 MODE CMD_GUIDED_DES_RDR
         AVERAGE VELOCITY=    3375.35
         TERMINAL VELOCITY=    2545.36
         LAUNCHER POSITION: X= -113879.58 Y=  -16624.14 Z=   24476.13
         FPOLE=   39848.34
         GUIDANCE TRACKBANK     1 HAS TRACK ON FUZEE    SEEKERS CONTRIBUTING  INFRA-RED
         ENDGAME SEEKER: IR   1
 A/C  2 IS KILLED AT TIME  82.907
 MISSILE 010102 FUZES AGAINST  3       RNG=      0.0 T=  82.846
         TERMINAL POSITION: X=  -75933.67 Y=   -4469.98 Z=   25000.09
         LAUNCH POSITION: X= -127017.19 Y=  -12934.89 Z=   25506.47 MODE CMD_GUIDED_DES_RDR
         AVERAGE VELOCITY=    3375.35
         TERMINAL VELOCITY=    2545.36
         LAUNCHER POSITION: X= -113879.58 Y=  -16624.14 Z=   24476.13
         FPOLE=   39848.34
         GUIDANCE TRACKBANK     1 HAS TRACK ON FUZEE    SEEKERS CONTRIBUTING  INFRA-RED
         ENDGAME SEEKER: IR   1
 A/C  3 IS KILLED AT TIME  82.907

NOTE: The above example has is how the strings would always be formatted. There may be many more than just these two, but they will always follow the above format.

I want to split this string on the "A/C 2 IS KILLED AT TIME 82.907" is killed part, but keep the delimiter.

My desired output for this example would be as follows:

['MISSILE 010102 FUZES AGAINST  2       RNG=      0.0 T=  82.846
         TERMINAL POSITION: X=  -75933.67 Y=   -4469.98 Z=   25000.09
         LAUNCH POSITION: X= -127017.19 Y=  -12934.89 Z=   25506.47 MODE CMD_GUIDED_DES_RDR
         AVERAGE VELOCITY=    3375.35
         TERMINAL VELOCITY=    2545.36
         LAUNCHER POSITION: X= -113879.58 Y=  -16624.14 Z=   24476.13
         FPOLE=   39848.34
         GUIDANCE TRACKBANK     1 HAS TRACK ON FUZEE    SEEKERS CONTRIBUTING  INFRA-RED
         ENDGAME SEEKER: IR   1
 A/C  2 IS KILLED AT TIME  82.907', 'MISSILE 010102 FUZES AGAINST  3       RNG=      0.0 T=  82.846
         TERMINAL POSITION: X=  -75933.67 Y=   -4469.98 Z=   25000.09
         LAUNCH POSITION: X= -127017.19 Y=  -12934.89 Z=   25506.47 MODE CMD_GUIDED_DES_RDR
         AVERAGE VELOCITY=    3375.35
         TERMINAL VELOCITY=    2545.36
         LAUNCHER POSITION: X= -113879.58 Y=  -16624.14 Z=   24476.13
         FPOLE=   39848.34
         GUIDANCE TRACKBANK     1 HAS TRACK ON FUZEE    SEEKERS CONTRIBUTING  INFRA-RED
         ENDGAME SEEKER: IR   1
 A/C  3 IS KILLED AT TIME  82.907']

I tried these regex patterns which are pretty close:

str.split(/[ ]+A\/C[ ]+[0-9]{1,2}[ ]+IS[ ]+KILLED[ ]+AT[ ]+TIME[ ]+[0-9]{0,4}\.[0-9]{0,4}/)

This gives me the text preceding the pattern, but of course the pattern matching text is removed. jsfiddle demo: https://jsfiddle.net/JordanCastillo/au7ontxf/1/

This one keeps the pattern, but it is a separate member of the array:

str.split(/([ ]+A\/C[ ]+[0-9]{1,2}[ ]+IS[ ]+KILLED[ ]+AT[ ]+TIME[ ]+[0-9]{0,4}\.[0-9]{0,4})/)

jsfiddle demo: http://jsfiddle.net/JordanCastillo/em7f1tqa/6/

After looking around, I found the following example:
https://stackoverflow.com/a/25221523/9861344
In the author's 4th example, he retains the delimiter on the text preceding it.

I tried replicating this, but as the comments indicate, it only works on single characters
https://jsfiddle.net/JordanCastillo/ot60azw1/1/

Toto
  • 89,455
  • 62
  • 89
  • 125

1 Answers1

0

One option is to use a positive lookbehind to assert the pattern, and as the format of the data is always the same, match 1+ whitespace chars to split on to remove the leading whitespace chars.

See the lookbehind support

(?<=[ ]A\/C[ ]+[0-9]{1,2}[ ]+IS[ ]+KILLED[ ]+AT[ ]+TIME[ ]+[0-9]{0,4}\.[0-9]{0,4})\s+

Regex demo

const regex = /(?<=[ ]A\/C[ ]+[0-9]{1,2}[ ]+IS[ ]+KILLED[ ]+AT[ ]+TIME[ ]+[0-9]{0,4}\.[0-9]{0,4})\s+/gm;
const str = `MISSILE 010102 FUZES AGAINST  2       RNG=      0.0 T=  82.846
         TERMINAL POSITION: X=  -75933.67 Y=   -4469.98 Z=   25000.09
         LAUNCH POSITION: X= -127017.19 Y=  -12934.89 Z=   25506.47 MODE CMD_GUIDED_DES_RDR
         AVERAGE VELOCITY=    3375.35
         TERMINAL VELOCITY=    2545.36
         LAUNCHER POSITION: X= -113879.58 Y=  -16624.14 Z=   24476.13
         FPOLE=   39848.34
         GUIDANCE TRACKBANK     1 HAS TRACK ON FUZEE    SEEKERS CONTRIBUTING  INFRA-RED
         ENDGAME SEEKER: IR   1
 A/C  2 IS KILLED AT TIME  82.907
 MISSILE 010102 FUZES AGAINST  3       RNG=      0.0 T=  82.846
         TERMINAL POSITION: X=  -75933.67 Y=   -4469.98 Z=   25000.09
         LAUNCH POSITION: X= -127017.19 Y=  -12934.89 Z=   25506.47 MODE CMD_GUIDED_DES_RDR
         AVERAGE VELOCITY=    3375.35
         TERMINAL VELOCITY=    2545.36
         LAUNCHER POSITION: X= -113879.58 Y=  -16624.14 Z=   24476.13
         FPOLE=   39848.34
         GUIDANCE TRACKBANK     1 HAS TRACK ON FUZEE    SEEKERS CONTRIBUTING  INFRA-RED
         ENDGAME SEEKER: IR   1
 A/C  3 IS KILLED AT TIME  82.907`;
console.log(str.split(regex));

Another option is to match for example the first line starting with MISSILE space and digit. Then match all lines that do not start with that same string or A/C. Finally end with your pattern.

You could match the optional leading whitespaces excluding newline using [^\S\r\n]* and then capture the rest of the match in group 1.

In the result you can access the group 1 value using m[1]

^[^\S\r\n]*(.*\bMISSILE \d.*(?:\r?\n(?!.*\b(?:MISSILE|A\/C) \d).*)*\r?\n[ ]+A\/C[ ]+[0-9]{1,2}[ ]+IS[ ]+KILLED[ ]+AT[ ]+TIME[ ]+[0-9]{0,4}\.[0-9]{0,4})

Regex demo

const regex = /^[^\S\r\n]*(.*\bMISSILE \d.*(?:\r?\n(?!.*\b(?:MISSILE|A\/C) \d).*)*\r?\n[ ]+A\/C[ ]+[0-9]{1,2}[ ]+IS[ ]+KILLED[ ]+AT[ ]+TIME[ ]+[0-9]{0,4}\.[0-9]{0,4})/gm;
const str = ` MISSILE 010102 FUZES AGAINST  2       RNG=      0.0 T=  82.846
         TERMINAL POSITION: X=  -75933.67 Y=   -4469.98 Z=   25000.09
         LAUNCH POSITION: X= -127017.19 Y=  -12934.89 Z=   25506.47 MODE CMD_GUIDED_DES_RDR
         AVERAGE VELOCITY=    3375.35
         TERMINAL VELOCITY=    2545.36
         LAUNCHER POSITION: X= -113879.58 Y=  -16624.14 Z=   24476.13
         FPOLE=   39848.34
         GUIDANCE TRACKBANK     1 HAS TRACK ON FUZEE    SEEKERS CONTRIBUTING  INFRA-RED
         ENDGAME SEEKER: IR   1
 A/C  2 IS KILLED AT TIME  82.907
 MISSILE 010102 FUZES AGAINST  3       RNG=      0.0 T=  82.846
         TERMINAL POSITION: X=  -75933.67 Y=   -4469.98 Z=   25000.09
         LAUNCH POSITION: X= -127017.19 Y=  -12934.89 Z=   25506.47 MODE CMD_GUIDED_DES_RDR
         AVERAGE VELOCITY=    3375.35
         TERMINAL VELOCITY=    2545.36
         LAUNCHER POSITION: X= -113879.58 Y=  -16624.14 Z=   24476.13
         FPOLE=   39848.34
         GUIDANCE TRACKBANK     1 HAS TRACK ON FUZEE    SEEKERS CONTRIBUTING  INFRA-RED
         ENDGAME SEEKER: IR   1
 A/C  3 IS KILLED AT TIME  82.907`;
let m;

let result = [];
while ((m = regex.exec(str)) !== null) result.push(m[1]);
console.log(result);
The fourth bird
  • 154,723
  • 16
  • 55
  • 70