I'm trying to replace occurrences of text in documents with a ugly but normally working regex (tested on regex101 and in the editor, it's matching). But when I run the code, it's not working.
Regex101: https://regex101.com/r/rGmYpP/1
My code:
(async () => {
const rename = util.promisify(fs.rename);
const files = await fs.readdir('./working');
for (let i = 0; i < files.length; i++) {
const file = files[i];
const read = async (filePath) => {
const str = fs.createReadStream(filePath, 'utf8')
.pipe(new stream.Transform({
decodeStrings : false,
transform(chunk, encoding, done) {
let result = chunk.replace(/\(LAST.*\n.*\n.*\n.*UPDATE/gm, '--------------');
done(null, result);
}
}));
const tempPath = await tempWrite(str);
await rename(tempPath, filePath+'-output');
};
await read('./working/'+file);
}
})();
Text sample:
OESHNF+Arial*1 [12 0 0 -12 0 0 ]msf
321.639 19.075 mo
(LAST )
[6.672 8.00409 8.00409 7.33191 0 ]xsh
354.938 19.075 mo
(UPDATE )
[8.664 8.00409 8.664 8.00409 7.33191 8.00409 0 ]xsh
406.945 19.075 mo
(OF )
Expected output:
OESHNF+Arial*1 [12 0 0 -12 0 0 ]msf
321.639 19.075 mo
-------------- )
[8.664 8.00409 8.664 8.00409 7.33191 8.00409 0 ]xsh
406.945 19.075 mo
(OF )
Thank you for your help, hope I provided everything.
console.log(chunk)to see what you have in the chunk. It might be that you don't have all the text for the regex to match..*afterUPDATE:/\(LAST.*\n.*\n.*\n.*UPDATE.*/gmunless you want that unmatched closing bracket.