I'm having a huge difficulty in creating a program to check the number of occurrences of a document based on rules set by me. With the help of regex, I check some fields, and if a particular field exists , I can count the number of occurrences of it, or I create a deeper scan. It's a little confusing, and I do not know exactly how to explain.
I 'm checking text files, but to reduce the complexity , I will use arrays.
I have the following array:
let strings = [
'COMPANY: NAME ID: 12',
'COMPANY: NAME ID: 12',
'COMPANY: NAME ID: 12',
'COMPANY: NAME2 ID: 10'
];
And this is the desire output:
{
'NAME' : { '12': 3 },
'NAME2': { '10': 1 }
}
To achieve this, I need to do some checks, so I came up with the following 'MAP':
let patterns = [
{
'pattern': 'COMPANY:\s*?([\w]+)\s',
'modifier': ''
},
{
'pattern' : 'ID:\s*?(\d{2})\s*',
'modifier' : ''
}
];
I 'm having a hard time creating the pseudo- code, I know it's something that can be done recursively, but I'm stuck . The biggest problem is because of nested, I can have several levels of nested, not necessarily two.
In the last hours I created the following code:
'use strict';
let patterns = [
{
'pattern': 'COMPANY:\s*?([\w]+)\s',
'modifier': ''
},
{
'pattern' : 'ID:\s*?(\d{2})\s*',
'modifier' : ''
}
];
let strings = [
'COMPANY: NAME ID: 12',
'COMPANY: NAME ID: 12',
'COMPANY: NAME ID: 12',
'COMPANY: NAME2 ID: 10'
];
var _data = {};
for (let string of strings) {
var root = _data;
for (let i = 0, length = patterns.length; i < length; i++) {
let item = patterns[i];
let regex = new RegExp(item.pattern, item.modifier);
let result = regex.exec(string);
if (i < patterns.length -1) {
root = root[result[1]] = {};
} else {
root = root[result[1]] = 1;
}
}
}
document.body.innerHTML = JSON.stringify({_data});
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…