java - 根据特定字符序列拆分字符串
问题描述
所以我的输入字符串如下所示:
《O2TV、SportTV》、Netflix/603605506、2016-01-02 15:15:01
年度订阅,Netflix /602602602,2016-01-02 10:55:32
权力的游戏, Netflix /602602602, 2016-01-02 09:49:09
我正在扭转它们并试图分裂
line = StringService.reverseIt(line);//reversing line so we can split it from end
String[] splitString = line.split("([ ./])", 5);
但是因为我的正则表达式不正确,所以我的拆分不正确,它看起来像这样:
时间是 = 22:13:11
数据为=2016-02-29
电话是= 604606321,
提供者是 =
注意是= 987654321,Netflix的充电
如果我在正则表达式中只留下空格,它的拆分是正确的,但我的字符串两侧有不需要的字符。所有输入数据从后向都有相同的模式,它是:空格(),然后是逗号(,),然后是斜杠(/),然后是逗号(,)。我需要使用的正确正则表达式是什么?谢谢大家!
解决方案
因为这个评论太长了......
当使用您选择的任何库或自定义解决方案将这些行视为 CSV 时,您将以一种或另一种形式获得以下内容(在这些示例中,line[]
只是一个简单的String[]
):
示例 1:
"O2TV, SportTV", Netflix /603605506, 2016-01-02 15:15:01
line[0] = "O2TV, SportTV"
line[1] = Netflix /603605506
line[2] = 2016-01-02 15:15:01
示例 2:
yearly subscription, Netflix /602602602, 2016-01-02 10:55:32
line[0] = yearly subscription
line[1] = Netflix /602602602
line[2] = 2016-01-02 10:55:32
示例 3:
game Of thrones, Netflix /602602602, 2016-01-02 09:49:09
line[0] = game Of thrones
line[1] = Netflix /6026026022
line[2] = 2016-01-02 09:49:09
从您想要的输出中,我猜这line[0]
始终是provider
.
电话号码始终是line[1].substring(line[1].indexOf('/'))
Netflix 可以通过以下方式提取line[1].substring(0,line[1].indexOf('/'))
该字符串recharging of 987654321
不包含在任何示例中。
对于时间和日期部分,您要么只创建一个LocalDateTime
对象line[2]
,然后使用 aDateTimeFormatter
将日期和时间从中提取到个人中String
,要么您也可以使用substring
:
String date = line[2].substring(0,line[2].indexOf(' '));
String time = line[2].substring(line[2].indexOf(' '));
问题:已解决。
根本不需要反转字符串。解析这些解析值所需的所有逻辑都是通过使用String 类的substring
和方法完成的。indexOf
不需要正则表达式。
推荐阅读
- reactjs - 如何在 React 应用程序中通过 Redux 处理登录错误
- firebase - Firebase 安全规则 - 在函数中使用 get
- java - 我们可以将 Chronounit 配置为从周六到周五安排一周吗
- dart - 在外面敲击时使底部表消失
- javascript - this.state.users 是一个数组,但仍然不会在 FlatList 中呈现
- c# - 按索引在特定行中插入值
- mql4 - 如何动态分配变量?
- javascript - 在最后一个选择验证不起作用
- vb.net - 从字符串列表中删除重复项并删除键值
- javascript - 是否可以在 javascript 中制作自己的自定义原生对象?