繁体   English   中英

逐行读取文本文件并执行function?

[英]Read text file line by line and execute a function?

我有这段代码,它检查像这样的逗号分隔字符串

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

如果逗号内的值与正则表达式中指定的值相同,则返回值 false 或 true。

当我尝试编写脚本以逐行读取 a.txt 文件并使用此正则表达式代码检查其真假时,我的问题就来了。

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;
  }
}

无法发布我现在尝试的代码,因为我没有,甚至不知道如何开始

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;
}
}

您可以使用 ReadLine 模块逐行读取文件。 您可以 append 列出测试结果,然后在文件完全读取后对其进行处理:

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;
    }
}

我将您所有的正则表达式组合成一个正则表达式:

/[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

使用该正则表达式来测试你的句子。 不要拆分字符串,而是将其视为一个整体

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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