[英]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.