gpt4 book ai didi

java - 如何在java中提取两个字符之间的数据

转载 作者:行者123 更新时间:2023-12-01 19:47:53 27 4
gpt4 key购买 nike

String text = "/'Team1 = 6', while /'Team2 = 4', and /'Team3 = 2'";

String[] body = text.split("/|,");
String b1 = body[1];
String b2 = body[2];
String b3 = body[3];

期望的结果:

b1 = 'Team1 = 6'
b2 = 'Team2 = 4'
b3 = 'Team3 = 2'

最佳答案

使用正则表达式。像这样的事情:

String text = "/'Team1 = 6', while /'Team2 = 4', and /'Team3 = 2'";
Matcher m = Pattern.compile("(\\w+\\s=\\s\\d+)").matcher(text);
// \w+ matches the team name (eg: Team1). \s=\s matches " = " and \d+ matches the score.
while (m.find()){
System.out.print(m.group(1)+"\n");
}

打印:

团队 1 = 6

团队 2 = 4

团队 3 = 2

关于java - 如何在java中提取两个字符之间的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52461921/

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