gpt4 book ai didi

java - 根据位置将字符串解析为标记

转载 作者:行者123 更新时间:2023-12-02 00:45:13 25 4
gpt4 key购买 nike

我有这个字符串

“姓名,演出于 14/08/09 开始,您的门票已于 14/08/09 预订”

在此字符串中,我想获取 StartDate、bookedDate 和 nameofthePerson 值作为单独的标记。这应该适用于相同格式的所有字符串

我如何在java中解析它们?

最佳答案

因此,如果格式相同,您可以使用正则表达式并将值收集到组中。

类似这样的事情:

String input = "Name, Show starts on 14/08/09, your ticket is booked on 14/08/09";
String regex = "([a-zA-Z \t]*),.*(\\d\\d/\\d\\d/\\d\\d),.*(\\d\\d/\\d\\d/\\d\\d)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher( input );
if( matcher.matches() && matcher.groupCount() == 4) //group 0 is always the entire expression
{
String name = matcher.group(1);
String startDate = matcher.group(2);
String bookedDate = matcher.group(3);
}

关于java - 根据位置将字符串解析为标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5172557/

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