How do I save the output of a REGEX to a new file

Asked

Viewed 68 times

3

const dirfile = 'BIG_DATA.HTM'
const reg = /(\<td .*)/g
const fs = require('fs')

const myReadStream =fs.createReadStream(dirfile,'utf8');


myReadStream.on('data',(chunk_)=>{

    var myReturn = chunk_.match(reg)

    //console.log(typeof(myReturn)) // -->>  returns all of My REGEX as object

    console.log(myReturn)  // -->> Returns every object of My REGEX that i nedd to write in another file 

}

1 answer

3


According to the documentation, the return of the method match, when regex has the flag g, is a array with all occurrences found (in this case, an array of strings).

So myReturn is an array, and just scroll through it to get the pouch and use createWriteStream to write your content in a file:

const entrada = fs.createReadStream(arquivoEntrada, 'utf8');
const saida = fs.createWriteStream(arquivoSaida, 'utf8');

entrada.on('data', (chunk_) => {    
    var myReturn = chunk_.match(reg);
    myReturn.forEach(function(trechoEncontrado) { saida.write(trechoEncontrado + '\n'); });    
});

saida.end();

Only one addendum: a regex search for <td and then we have .*, which is "zero or more characters". Only the point corresponds to any character, except line breaks, and the quantifier * is, by default, "greedy" and tries to grab as many characters as possible.

This means that the regex will catch from the first <td which find, and then will take everything up to the next line break (i.e., depending on the contents of the file, it may pick up more than just the tag td). If the goal was to catch only the td, it already leaves a little of the scope of the question, and it will depend on how is this HTML (because use regex to Parsing html is not always the best solution).

  • 1

    Legal worked!!!!

  • const output = Fs.createWriteStream(fileSaida, 'utf8'); const input = Fs.createReadStream(fileTutf8'); input.on('data', (chunk_) => { var myReturn = chunk_.match(reg); myReturn.foreach(Function(trechoEntrated) { output.write(trechoEntrated + ' n'); }); });

  • I just had to take the exit.();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.