简体   繁体   中英

Read text file line by line and execute a function?

I have this code that it checks a comma separated strings like this

85aecb80-ac00-40e3-813c-5ad62ee93f42,1813724,client@gmail.com
13vg4f20-fc24-604f-2ccc-1af23taf4421,4255729,developer@gmail.com

and returns a value false or true if values inside commas are the same than the ones specified on the regex.

My problem it comes when I try to do a script for read a.txt file line by line and use this regex code to check if its true or false.

function test(str){

  let regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; // email regex

  str = str.split(","); 

  // string should be of length 3 with str[1] number of length 7
  if(str && str.length === 3 && Number(str[1]) && str[1] ){

    let temp = str[0].split("-");

    // check for 85aecb80-ac00-40e3-813c-5ad62ee93f42 separately.
    if(temp && temp.length === 5 &&  /[a-zA-Z\d]{8}/.test(temp[0]) &&  /[a-zA-Z\d]{4}/.test(temp[1]) &&  /[a-zA-Z\d]{4}/.test(temp[2]) &&  /[a-zA-Z\d]{4}/.test(temp[3]) &&  /[a-zA-Z\d]{12}/.test(temp[4])){

      // email regex
      if(regex.test(str[2])){
        return true;

      } 
      else{
        return false;
      }

    }
    else{

      return false
    }
  }
  else{

    return false;
  }
}

Can't post code I tried now because I didn't, not even a clue how to start

var fs = require('fs');
const readline = require('readline');
const readInterface = readline.createInterface({
input: fs.createReadStream('read.txt'),
output: process.stdout,
console: false
});

readInterface.on('line', function (line) {
  console.log(line);
  test(line);
});
function test(str) {

 let regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; // email regex

 str = str.split(",");

 // string should be of length 3 with str[1] number of length 7
if (str && str.length === 3 && Number(str[1]) && str[1]) {

   let temp = str[0].split("-");

   // check for 85aecb80-ac00-40e3-813c-5ad62ee93f42 separately.
if (temp && temp.length === 5 && /[a-zA-Z\d]{8}/.test(temp[0]) && /[a-zA-Z\d]{4}/.test(temp[1]) && /[a-zA-Z\d]{4}/.test(temp[2]) && /[a-zA-Z\d]{4}/.test(temp[3]) && /[a-zA-Z\d]{12}/.test(temp[4])) {

     // email regex
  if (regex.test(str[2])) {
    return true;
  }
  else {
    return false;
  }

   }
else {
    return false;
}
 }else {
    return false;
}
}

You can use the ReadLine module to read files line by line. You can append a list of the test results and then do something with them when the file is completely read:

const readline = require('readline');
const fs = require('fs');
const inputFileName = './testfile.txt';

const readInterface = readline.createInterface({
    input: fs.createReadStream(inputFileName),
});

let testResults = [];
readInterface.on('line', line => {
    testResult = test(line);
    console.log(`Test result (line #${testResults.length+1}): `, testResult);
    testResults.push({ input: line, testResult } );
});

// You can do whatever with the test results here.
readInterface.on('close', () => {
    console.log("Test results:", testResults);
});

function test(str){

    let regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; // email regex

    str = str.split(","); 

    // string should be of length 3 with str[1] number of length 7
    if(str && str.length === 3 && Number(str[1]) && str[1] ) {

        let temp = str[0].split("-");

        // check for 85aecb80-ac00-40e3-813c-5ad62ee93f42 separately.
        if(temp && temp.length === 5 &&  /[a-zA-Z\d]{8}/.test(temp[0]) &&  /[a-zA-Z\d]{4}/.test(temp[1]) &&  /[a-zA-Z\d]{4}/.test(temp[2]) &&  /[a-zA-Z\d]{4}/.test(temp[3]) &&  /[a-zA-Z\d]{12}/.test(temp[4])){

            // email regex
            if(regex.test(str[2])) {
                return true;
            } else {
                return false;
            }
        } else { 
            return false
        }
    } else {
        return false;
    }
}

I combined all your regexes into one single regex:

/[a-zA-Z\d]{8}\-[a-zA-Z\d]{4}\-[a-zA-Z\d]{4}\-[a-zA-Z\d]{4}\-[a-zA-Z\d]{12},\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/gm

Use that regex to test your sentence. Do not split the string but treat it as a whole

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM