ad

解析日志文件以使用正则表达式显示来自多行的数据-英雄云拓展知识分享

匿名投稿 282 2024-01-22

因此,这种情况下,我试图在此处解析一些代码,以从日志文件中获得消息文本。我去解释。这是代码:

// Print to interactions

try

{

// assigns the input file to a filereader object

解析日志文件以使用正则表达式显示来自多行的数据-英雄云拓展知识分享

BufferedReader infile = new BufferedReader(new FileReader(log));

sc = new Scanner(log);

while(sc.hasNext())

{

String line=sc.nextLine();

if(line.contains("LANTALK")){

Document doc = Jsoup.parse(line);

Element idto = doc.select("MBXTO").first();

Element msg = doc.select("MSGTEXT").first();

System.out.println(" to " + idto.text() + " " +

msg.text());

System.out.println();

} // End of if

} // End of while

try

{

// Print to output file

sc = new Scanner (log);

while(sc.hasNext())

{

String line=sc.nextLine();

if(line.contains("LANTALK")){

Document doc = Jsoup.parse(line);

Element idto = doc.select("MBXTO").first();

Element msg = doc.select("MSGTEXT").first();

outFile.println(" to " + idto.text() + " " +

msg.text());

outFile.println();

outFile.println();

} // End of if

} // End of while

} // end of try

我正在从日志文件中获得输入,以下是它的外观示例和我要过滤的行:

08:25:20.740 [D] [T:000FF0] [F:LANTALK2C] <CMD>LANMSG</CMD>

<MBXID>1124</MBXID><MBXTO>5760</MBXTO><SUBTEXT>LanTalk</SUBTEXT><MOBILEADDR>

</MOBILEADDR><LAP>0</LAP><SMS>0</SMS><MSGTEXT>and I talked to him and he

gave me a credit card number</MSGTEXT>

08:25:20.751 [+] [T:000FF0] [S:1:1:1124:5607:5] LANMSG [15/2 | 0]

08:25:20.945 [+] [T:000FF4] [S:1:1:1124:5607:5] LANMSGTYPESTOPPED [0/2 | 0]

08:25:21.327 [+] [T:000FE8] [S:1:1:1124:5607:5] LANMSGTYPESTARTED [0/2 | 0]

到至今为止为止,我已能够过滤包括消息的行(LANMSG)。由此,我能够取得收件人的ID号(MBXTO)。但是下一行包括发件人的ID,我需要拉出并显示。 (([S:1:1:1124:SENDERID:5])。我应当怎样做?以下是我将取得的输出的副本:

to 5760 and I talked to him and he gave me a credit card number

这就是我需要得到的:

SENDERID to 5760 and I talked to him and he gave me a credit card number

任何帮助你们都可以给我这一切都会很棒。我只是不肯定如何获得所需的信息。

看答案

您的答案还不够清楚,但是由于您仿佛在此代码中没有使用过正则是...请记住要在询问之前指定您尝试过的内容。不管如何,您要搜索的正则是:

(\d{2}:\d{2}:\d{2}\.\d{3})\s\[D\].+<MBXID>(\d+)<\/MBXID><MBXTO>(\d+)<\/MBXTO>.+<MSGTEXT>(.+)<\/MSGTEXT>

REGEX101中的工作示例
它应当捕获:
$1: 08:25:20.740
$2: 1124
$3: 5760
$4: and I talked to him and hegave me a credit card number (请注意,它还捕获\ n或newline,字符)。
(另外,您将使用 matcher.group(number) 代替 $number 在Java)。

然后,您可使用这些替换(组参考)术语来获得格式化的输出。

例如。: $1 [$2] to [$3] $4

应当返回:

08:25:20.740 [1124] to [5760] and I talked to him and he

gave me a credit card number

请记住,当您打算在Java代码中实现正则表达式时,必须逃脱所有的后斜切(\),因此,这种情况下,这条正则是更大的:

Pattern pattern = Pattern.compile("(\\d{2}:\\d{2}:\\d{2}\\.\\d{3})\\s\\[D\\].+<MBXID>(\\d+)<\\/MBXID><MBXTO>(\\d+)<\\/MBXTO>.+<MSGTEXT>(.+)<\\/MSGTEXT>", Pattern.MULTILINE + Pattern.DOTALL);

// Multiline is used to capture the LANMSG more than once, and Dotall is used to make the '.' term in regex also match the newline in the input

Matcher matcher = pattern.matcher(input);

while (matcher.find()){

String output = matcher.group(1) + " [" + matcher.group(2) + "] to [" + matcher.group(3) + "] " + matcher.group(4);

System.out.println(output);

}

对您的第2个问题 哦,您已编辑并删除它。 。 。但是我依然回答:您可以解析 $2$3 并让他们返回一个整数:

int id1 = Integer.parseInt(matcher.group(2));

int id2 = Integer.parseInt(matcher.group(3));

这样,您可以创建一种方法来返回这些ID的名称。例如。:
UserUtil.getName(int id)


🚀🌟 点击注册 免费试用超级应用平台-英雄云企业级hpapaas 🌟🚀 😃👉🌐

免责声明:

本网址(www.yingxiongyun.com)发布的材料主要源于独立创作和网友匿名投稿。此处提供的所有信息仅供参考之用。我们致力于提供准确且可信的信息,但不对材料的完整性或真实性作出任何保证。用户应自行验证相关信息的正确性,并对其决策承担全部责任。对于由于信息的错误、不准确或遗漏所造成的任何损失,本网址不承担任何法律责任。本网站所展示的所有内容,如文字、图像、标志、音频、视频、软件和程序等的版权均属于原创作者。如果任何组织或个人认为网站内容可能侵犯其知识产权,或包含不准确之处,请即刻联系我们进行相应处理。

标签:爪哇 正则
上一篇:简单的VBA选择一个从第一个.find值选择的范围?-英雄云拓展知识分享
下一篇:如何在JavaScript中使用DOM访问列表值?-英雄云拓展知识分享
相关文章

 发表评论

暂时没有评论,来抢沙发吧~

×