-1

I am using a code that pulls a data between two strings.

Below is my code works fine but i got a problem with it:

function pullAllDataBetween(file_data, str1, str2) {

var string_nodes = [];
var append = false;

for (var i = 0; i < file_data.length; i++) {
  if (file_data[i] === str1) {
    append = true;
    continue;
  } else if (file_data[i] === str2) {
    append = false;
    break;
  } 

  if (append) {
    string_nodes.push(file_data[i]);
  }
 }

 return string_nodes;
}


Then use it with this 

const data = file_data.split("\r\n");

const string_nodes = pullAllDataBetween(data, "BEGINNING", "ENDING" );

It successfully pulls out the data from this kind of format:

1 BEGINNING
2 1 1 0 0; 2 4 0 0; 3 7 0 0; 4 0 0 -1; 5 1 0 -1; 6 1 0.75 -1; 7 4 0 -1;
3 8 4 0.75 -1; 9 7 0 -1; 10 7 0.75 -1; 11 8 0 -1; 12 0 0 -4; 13 1 0 -4;
4 14 1 0.75 -4; 15 4 3 -4; 16 7 0 -4; 17 7 0.75 -4; 18 8 0 -4; 19 0 0 -7;
5 ENDING

Result

 ['1 1 0 0; 2 4 0 0; 3 7 0 0; 4 0 0 -1; 5 1 0 -1; 6 1 0.75 -1; 7 4 0 -1;',
  '8 4 0.75 -1; 9 7 0 -1; 10 7 0.75 -1; 11 8 0 -1; 12 0 0 -4; 13 1 0 -4;',
  '14 1 0.75 -4; 15 4 3 -4; 16 7 0 -4; 17 7 0.75 -4; 18 8 0 -4; 19 0 0 -7;']

Then i have this kind of format :

  1   START JOB INFORMATION
  2   DATE 23-May-18
  3   END JOB INFORMATION
  4   INPUT WIDTH 79
  5   UNIT METER KN
  6   BEGINNING
  7   5 13 16 22 24 ENDING
  8   ISOTROPIC STEELAPPROX
  9   E 2e+008
  10  POISSON 0.27
  11  DENSITY 77.01
  12  ALPHA 1.2e-005

now i cant make use of my code. I got stuck.

i am trying to get a result like the one below:

['5; 13; 16; 22; 24;']

thanks for those who will help.

Tramyer
  • 19
  • 4

2 Answers2

0
function pullAllDataBetween(file_data, str1, str2) {

    return file_data.split(str1)[1].split(str2)[0].trim()
}

If you know that str1 comes before str2, you can just do the above. The first split and election of the element at index 1 will isolate the substring of file_data after str1, the second split will isolate the substring of the first substring that comes before str2 (so now you've just got the string in between str1 and str2). trim() will remove leading and trailing whitespaces (including new lines) so that if there was space or newline in between the str1/str2 and the first number, that'll be disregarded.

Then you can use file_data (the unsplit string) directly:

const match_string = pullAllDataBetween(file_data, "BEGINNING", "ENDING");

And finally, split the match string as in the example.

const string_nodes = match_string.split("\r\n");

So then this is what your code would look like:

var file_data = "BEGINNING \r\n  1 1 0 0; 2 4 0 0; 3 7 0 0; 4 0 0 -1; 5 1 0 -1; 6 1 0.75 -1; 7 4 0 -1; \r\n 8 4 0.75 -1; 9 7 0 -1; 10 7 0.75 -1; 11 8 0 -1; 12 0 0 -4; 13 1 0 -4; \r\n 14 1 0.75 -4; 15 4 3 -4; 16 7 0 -4; 17 7 0.75 -4; 18 8 0 -4; 19 0 0 -7; \r\n ENDING";
var match_string = pullAllDataBetween(file_data, "BEGINNING", "ENDING" );
var string_nodes = match_string.split("\r\n");

output: ["1 1 0 0; 2 4 0 0; 3 7 0 0; 4 0 0 -1; 5 1 0 -1; 6 1 0.75 -1; 7 4 0 -1; ", " 8 4 0.75 -1; 9 7 0 -1; 10 7 0.75 -1; 11 8 0 -1; 12 0 0 -4; 13 1 0 -4; ", " 14 1 0.75 -4; 15 4 3 -4; 16 7 0 -4; 17 7 0.75 -4; 18 8 0 -4; 19 0 0 -7;"]

And for the second example:

var file_data = "BEGINNING \r\n  5 13 16 22 24 ENDING";
var match_string = pullAllDataBetween(file_data, "BEGINNING", "ENDING" );
var string_nodes = match_string.split("\r\n");
string_nodes;

output: ["5 13 16 22 24"]

Jonas
  • 1,473
  • 2
  • 13
  • 28
  • With your function i am having an error. I've done a little experiment with the code i'm using. It can read the first linefeed. But why in the world i can't read the last wording in the linefeed. Cause what i did is i tried to put the ENDING brought it down and it successfully read the data. But when i tried to get them in a same line it doesn't read the "ENDING". – Tramyer Jun 20 '18 at 03:23
  • I updated my answer with the exact strings and results. How is the output different from what you expected? – Jonas Jun 20 '18 at 03:46
  • the code worked but it doesn't work in the other file i'm currently working on. but there's no semicolon to your output mate. – Tramyer Jun 20 '18 at 03:57
  • Can you post the file_data string? My hunch is that that's a different format. – Jonas Jun 20 '18 at 03:57
  • i added the info please see above. – Tramyer Jun 20 '18 at 05:27
0

Is this what you are looking for? Granted, it doesn't do any checking of validity etc.. but something like this seems to get me the result you asked for. If not, my bad. Just thought to give it a go.

var file_data = `1 BEGINNING
2 5 13 16 22 24 ENDING`;

function pullAllDataBetween(data, start, end) {
  var newReg1 = new RegExp(`${start}(.*?)${end}`);

  return Array.of(data.replace(/\n/g,"")
         .replace(/\r/g,"")
         .match(newReg1)[1]
         .trim()
         .split(' ')
         .join(' '))

}
const match_string = pullAllDataBetween(file_data, "BEGINNING", "ENDING");
     // ["2 5 13 16 22 24"]
james emanon
  • 11,185
  • 11
  • 56
  • 97
  • thanks for the code mate will use it the future. The above code from jonas works. Is just that the file im working with doesn't read my txt file. But try to figure it out myself. – Tramyer Jun 20 '18 at 04:17