gpt4 book ai didi

java - 如何从给定字符串获取描述中的预期输出

转载 作者:行者123 更新时间:2023-11-30 06:10:57 24 4
gpt4 key购买 nike

[评分]:Bot-16 从 [Actor - Kill] 处获得 2.0 赏金 [受害者:Actor200_(Actor_Local_Minion_200)] [@152133]

如何格式化上述字符串以便获得以下输出:

玩家 ID = Bot-16

积分 = 2.0

Action = [ Actor -杀死]

受害者 = Actor200_(Actor_Local_Minion_200)

时间戳 = 152133

还有一些行没有受害者标签,如下面的字符串

[评分]:2 Dev 158744780 从 [Actor - Kill] [Ace] [@519382] 获得 20.0 赏金

输出应该是

玩家 ID = 2 Dev 158744780

积分 = 20.0

Action = Actor-Kill

受害者=艾斯

时间戳 = 519382

最佳答案

您可以使用正则表达式来处理这个问题。

查看您的输入字符串,我想出了这个正则表达式:

\[Scoring\]: (.*) got ([^\s]*) bounty from \[([^\]]*)\] \[(?:Victim: )?([^\]]*)\] \[@([^\]]*)\]

此处演示:https://regex101.com/r/e3zuKw/2

这是 Java 中的演示程序及其输出。我认为这将是您的解决方案。

输出:

**First Input**
Player-ID = Bot-16
Points = 2.0
Action = Actor - Kill
Victim = Actor200_(Actor_Local_Minion_200)
Timestamp = 152133

**Second Input**
Player-ID = 2 Dev 158744780
Points = 20.0
Action = Actor - Kill
Victim = Ace
Timestamp = 519382

代码:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo {
public static void main(String[] args) {
System.out.println("**First Input**");
printInfo("[Scoring]: Bot-16 got 2.0 bounty from [Actor - Kill] [Victim: Actor200_(Actor_Local_Minion_200)] [@152133]");

System.out.println("\n**Second Input**");
printInfo("[Scoring]: 2 Dev 158744780 got 20.0 bounty from [Actor - Kill] [Ace] [@519382]");
}

public static void printInfo(String line) {
String pattern = "\\[Scoring\\]: (.*) got ([^\\s]*) bounty from \\[([^\\]]*)\\] \\[(?:Victim: )?([^\\]]*)\\] \\[@([^\\]]*)\\]";

Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);

if (m.find()) {
System.out.println("Player-ID = " + m.group(1));
System.out.println("Points = " + m.group(2));
System.out.println("Action = " + m.group(3));
System.out.println("Victim = " + m.group(4));
System.out.println("Timestamp = " + m.group(5));
}
}
}

关于java - 如何从给定字符串获取描述中的预期输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50245507/

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