【问题标题】:Not pushing correct regex match to JSON using javascript未使用 javascript 将正确的正则表达式匹配推送到 JSON
【发布时间】:2021-10-19 19:34:57
【问题描述】:

概述:

我正在使用正则表达式来解析文本文档并创建 JSON 文档。该文档是从控制台日志中解析出来的。

似乎发生的事情是(regex_1_match && regex_2_match) 没有按预期工作。它似乎与 regex_1 匹配,并且看起来满足 regex_2 并将其保存在同一个数组中。

const fs = require('fs');
const filename = fs.readFileSync('test.txt').toString();
var regex_1 = /"Course([0-9.])"/g;
var regex_2 = /"(Name)"/g;
var regex_3 = /"(No Name)"/g;
var regex_1_match = filename.match(regex_1);
var regex_2_match = filename.match(regex_2);
var regex_3_match = filename.match(regex_3);

let testJSON = [];

//for each line item
for  (let index = 0; index < filename.length; index++) {
  if(regex_1_match && regex_2_match) {
    testJSON.push({
      Course: regex_1[index]
      Name: regex_2[index]
    });
  }
}
fs.writeFileSync("parsed_test_doc",JSON.stringify(testJSON));

test.txt:

------------ Course1 ------------
------------ foo ------------
------------ Name ------------
------------ Course2 ------------
------------ foo ------------
------------ No Name ------------
------------ Course3 ------------
------------ Name ------------
------------ foo ------------
------------ Course4 ------------
------------ No Name ------------
------------ Course5 ------------
------------ foo ------------
------------ Name ------------

输出:

[{
  "Course": "Course1",
  "Name": "Name"
}, {"Course": "Course2",
  "Name": "Name"
},{"Course": "Course3",
  "Name": "Name"
},{"Course": "Course4",
},{{"Course": "Course5"
}

预期输出:

[{
  "Course": "Course1",
  "Name": "Name"
}, {
  "Course": "Course2"
}, {
  "Course": "Course3",
  "Name": "Name"
}, {
  "Course": "Course4"
}, {
  "Course": "Course5",
  "Name": "Name"
}]

【问题讨论】:

  • 您确定您的for 循环正确吗?遍历字符串会遍历每个字符,而不是行
  • 另外,JSON.stringify() 无法生成您的 "output",因为它不是有效的 JSON
  • 为什么你的正则表达式包含"字符?它们不会出现在您的文本示例中
  • 对不起,我没有验证我的测试示例,我将验证。它是我目前正在使用的非常简化的版本,你说得对,应该删除 " 字符。我的开发版本中没有这个。

标签: javascript node.js json regex


【解决方案1】:

关于示例代码的几点说明

  • 在当前代码和示例文本中,如果 match 的结果(数组或空值)对于两个匹配项都为 true,则这部分 regex_1_match &amp;&amp; regex_2_match 为 true,这可能会给您带来意想不到的结果
  • 注意变量filename包含整个文件,所以这个循环中的index将是文件内容中每个字符的个数
  • 使用 index 来索引正则表达式 regex_1[index] 这种方式不起作用,也许您的意思是索引匹配
  • regex_3_match 从未使用过

您可能会做的是使用具有 2 个捕获组的单一模式,其中第 2 组捕获 Name 的第一次出现,它是可选的。

\b(Course\d+)(?:(?!Course\d)[^])*?(?:No Name|(Name)|(?=Course\d))

模式匹配:

  • \b一个字边界
  • (Course\d+)第 1 组 Course 和 1+ 位中捕获
  • (?:非捕获组
    • (?!Course\d)[^] 匹配任何字符,如果 Course 和 1+ 数字不直接在右侧
  • )*? 关闭非捕获组并可选择非贪婪地重复它
  • (?: 替代方案的非捕获组
    • No Name|(Name)|(?=Course\d) 匹配 No Name,或在 第 2 组 中捕获 Name 或断言下一个 Course 和一个数字以在不存在名称时继续匹配。
  • )关闭非捕获组

Regex demo

const fs = require('fs');
const filename = fs.readFileSync('test.txt').toString();
const regex = /\b(Course\d+)(?:(?!Course\d)[^])*?(?:No Name|(Name)|(?=Course\d))/g;
const result = Array.from(filename.matchAll(regex), m => {
    let res = {"Course": m[1]}
    if (undefined !== m[2]) {
        console.log("not undefined")
        res["Name"]=m[2];
    }
    return res;
});
console.log(result);

输出

[
  { Course: 'Course1', Name: 'Name' },
  { Course: 'Course2' },
  { Course: 'Course3', Name: 'Name' },
  { Course: 'Course4' },
  { Course: 'Course5', Name: 'Name' }
]

const s = `------------ Course1 ------------
------------ foo ------------
------------ Name ------------
------------ Course2 ------------
------------ foo ------------
------------ No Name ------------
------------ Course3 ------------
------------ Name ------------
------------ foo ------------
------------ Course4 ------------
------------ No Name ------------
------------ Course5 ------------
------------ foo ------------
------------ Name ------------`;
const regex = /\b(Course\d+)(?:(?!Course\d)[^])*?(?:No Name|(Name)|(?=Course\d))/g;
const result = Array.from(s.matchAll(regex), m => {
  let res = {
    "Course": m[1]
  }
  if (undefined !== m[2]) {
    res["Name"] = m[2];
  }
  return res;
});
console.log(result);

【讨论】:

  • 我认为他们的意思是迭代和索引匹配结果,而不是正则表达式。
猜你喜欢
  • 1970-01-01
  • 2021-10-06
  • 2017-03-20
  • 2014-07-12
  • 2011-11-29
  • 2012-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多