如何基于逗号分割字符串

时间:2021-06-26 00:24:19

I am having a String which is a combination of comma and space now to want to get specific elements in a order How i can do that ,I am posting the String

我有一个字符串,它是逗号和空格的组合,现在想要获得订单中的特定元素我怎么能这样做,我发布字符串

12:00am, 2:30am3:30am, 5:00am7:45pm,9:00pm

This is the String i am trying to split the String using

这是我试图使用分割字符串的字符串

List<String> interviewTimingToFrom1 = Arrays.asList(interviewTime1.split(","));

But this is not working ,i just want to split 12:00am,3:30am,7:45pm in a List and 2:30am,5:00am,9:00am in a list how to do that ,please somebody help

但是这不起作用,我只想在列表中的12:00 am,30:30,7:45 pm和上午2:30从上午5点30分在列表中拆分如何做到这一点,请有人帮忙

1 个解决方案

#1


2  

You can use this regex:

你可以使用这个正则表达式:

List<String> interviewTimingToFrom1 = Arrays.asList(
                    interviewTime1.split("\\s*,\\s*|(?<=[ap]m)(?=\\d)"));

RegEx Demo

RegEx演示

RegEx Breakup

RegEx分手

Split using:

拆分使用:

\s*,\s*          # comma surrounded by optional spaces on either sides
|                # regex alternation
(?<=[ap]m)(?=\d) # when preceding text is am or pm and following character is a digit

#1


2  

You can use this regex:

你可以使用这个正则表达式:

List<String> interviewTimingToFrom1 = Arrays.asList(
                    interviewTime1.split("\\s*,\\s*|(?<=[ap]m)(?=\\d)"));

RegEx Demo

RegEx演示

RegEx Breakup

RegEx分手

Split using:

拆分使用:

\s*,\s*          # comma surrounded by optional spaces on either sides
|                # regex alternation
(?<=[ap]m)(?=\d) # when preceding text is am or pm and following character is a digit