我正在使用在两个字符串之间提取数据的代码。
以下是我的代码可以正常工作,但是我遇到了问题:
function pullAllDataBetween(file_data, str1, str2) {
var string_nodes = [];
var append = false;
for (var i = 0; i < file_data.length; i++) {
if (file_data[i] === str1) {
append = true;
continue;
} else if (file_data[i] === str2) {
append = false;
break;
}
if (append) {
string_nodes.push(file_data[i]);
}
}
return string_nodes;
}
Then use it with this
const data = file_data.split("\r\n");
const string_nodes = pullAllDataBetween(data, "BEGINNING", "ENDING" );
它成功地从这种格式中提取数据:
1 BEGINNING
2 1 1 0 0; 2 4 0 0; 3 7 0 0; 4 0 0 -1; 5 1 0 -1; 6 1 0.75 -1; 7 4 0 -1;
3 8 4 0.75 -1; 9 7 0 -1; 10 7 0.75 -1; 11 8 0 -1; 12 0 0 -4; 13 1 0 -4;
4 14 1 0.75 -4; 15 4 3 -4; 16 7 0 -4; 17 7 0.75 -4; 18 8 0 -4; 19 0 0 -7;
5 ENDING
结果
['1 1 0 0; 2 4 0 0; 3 7 0 0; 4 0 0 -1; 5 1 0 -1; 6 1 0.75 -1; 7 4 0 -1;',
'8 4 0.75 -1; 9 7 0 -1; 10 7 0.75 -1; 11 8 0 -1; 12 0 0 -4; 13 1 0 -4;',
'14 1 0.75 -4; 15 4 3 -4; 16 7 0 -4; 17 7 0.75 -4; 18 8 0 -4; 19 0 0 -7;']
然后我有这种格式:
1 START JOB INFORMATION
2 DATE 23-May-18
3 END JOB INFORMATION
4 INPUT WIDTH 79
5 UNIT METER KN
6 BEGINNING
7 5 13 16 22 24 ENDING
8 ISOTROPIC STEELAPPROX
9 E 2e+008
10 POISSON 0.27
11 DENSITY 77.01
12 ALPHA 1.2e-005
现在我无法使用我的代码。我被卡住了。
我正在尝试获得类似以下结果:
['5; 13; 16; 22; 24;']
感谢那些会提供帮助的人。
答案 0 :(得分:0)
function pullAllDataBetween(file_data, str1, str2) {
return file_data.split(str1)[1].split(str2)[0].trim()
}
如果您知道str1早于str2,则可以执行上述操作。第一次拆分和选择索引1处的元素将隔离str1之后的file_data子字符串,第二次拆分将隔离str2之前的第一个子字符串的子字符串(因此,您现在已经在str1和str2之间找到了字符串)。 trim()将删除前导和尾随空格(包括换行符),以便在str1 / str2与第一个数字之间存在空格或换行符时,将不予考虑。
然后,您可以直接使用file_data(未分割的字符串):
const match_string = pullAllDataBetween(file_data, "BEGINNING", "ENDING");
最后,按照示例分割匹配字符串。
const string_nodes = match_string.split("\r\n");
那么这就是您的代码的样子:
var file_data = "BEGINNING \r\n 1 1 0 0; 2 4 0 0; 3 7 0 0; 4 0 0 -1; 5 1 0 -1; 6 1 0.75 -1; 7 4 0 -1; \r\n 8 4 0.75 -1; 9 7 0 -1; 10 7 0.75 -1; 11 8 0 -1; 12 0 0 -4; 13 1 0 -4; \r\n 14 1 0.75 -4; 15 4 3 -4; 16 7 0 -4; 17 7 0.75 -4; 18 8 0 -4; 19 0 0 -7; \r\n ENDING";
var match_string = pullAllDataBetween(file_data, "BEGINNING", "ENDING" );
var string_nodes = match_string.split("\r\n");
输出:["1 1 0 0; 2 4 0 0; 3 7 0 0; 4 0 0 -1; 5 1 0 -1; 6 1 0.75 -1; 7 4 0 -1; ", " 8 4 0.75 -1; 9 7 0 -1; 10 7 0.75 -1; 11 8 0 -1; 12 0 0 -4; 13 1 0 -4; ", " 14 1 0.75 -4; 15 4 3 -4; 16 7 0 -4; 17 7 0.75 -4; 18 8 0 -4; 19 0 0 -7;"]
第二个例子:
var file_data = "BEGINNING \r\n 5 13 16 22 24 ENDING";
var match_string = pullAllDataBetween(file_data, "BEGINNING", "ENDING" );
var string_nodes = match_string.split("\r\n");
string_nodes;
输出:["5 13 16 22 24"]
答案 1 :(得分:0)
这是您要找的东西吗?当然,它不会做任何有效性检查等。但是类似的事情似乎可以使我得到您所要求的结果。如果没有,我不好。只是想去尝试一下。
var file_data = `1 BEGINNING
2 5 13 16 22 24 ENDING`;
function pullAllDataBetween(data, start, end) {
var newReg1 = new RegExp(`${start}(.*?)${end}`);
return Array.of(data.replace(/\n/g,"")
.replace(/\r/g,"")
.match(newReg1)[1]
.trim()
.split(' ')
.join(' '))
}
const match_string = pullAllDataBetween(file_data, "BEGINNING", "ENDING");
// ["2 5 13 16 22 24"]