gpt4 book ai didi

Javascript:将文本文件读入数组,按正则表达式拆分

转载 作者:行者123 更新时间:2023-11-29 19:22:56 25 4
gpt4 key购买 nike

我需要解析一个看起来像日志的纯文本文件:

11/04/2015 11:45:01: James: Cheers guys, enjoy the weekend!

11/04/2015 12:08:55: Sarah‬: Sounds good James

11/04/2015 12:09:24: ‪Sarah: What are the details of the trip?

11/04/2015 12:19:06: Leah: Driving up on Friday.
Saturday we'll hit the beach.
Sunday paaaaarty!

11/04/2015 12:29:54: ‪James: Nice.

我目前正在按换行符进行解析:

var messages = data.split('\n');

但这在消息包含换行符的情况下不起作用(请参阅上面 Leah 的消息)。

解析每个新条目的正确方法是什么?某种正则表达式日期/时间匹配?还是上面提到的解析日期的一些正则表达式?

感谢您的帮助。

最佳答案

我认为你可以在这里尝试的是 -

如果每行统计数据都带有日期格式,则将其后面的部分作为字符串,直到它以另一种日期格式结束。

Dont split using\n instead use the date that is in mm/dd/yyyy hh:mm:ss: format .

Logic needs to applied for below type because your text is in this type as mentioned below--

Date Format starts >> content << Date Format Ends

使用本指南制作您自己的正则表达式。 http://www.w3schools.com/jsref/jsref_obj_regexp.asp

Try this Regular Expression to split  /[0-9]+\/[0-9]+\/[0-9]* [0-9]*\:[0-9]*\:[0-9]*\:/g



var re = /[0-9]+\/[0-9]+\/[0-9]* [0-9]*\:[0-9]*\:[0-9]*\:/g;
var str = '11/04/2015 11:45:01: James: Cheers guys, enjoy the weekend!\n\n11/04/2015 12:08:55: Sarah‬: Sounds good James\n\n11/04/2015 12:09:24: ‪Sarah: What are the details of the trip?\n\n11/04/2015 12:19:06: Leah: Driving up on Friday.\nSaturday we\'ll hit the beach.\nSunday paaaaarty!\n\n11/04/2015 12:29:54: ‪James: Nice.';
var m;

while ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}

关于Javascript:将文本文件读入数组,按正则表达式拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32308596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com