Java 使用正则表达式将字符串拆分为两个字符串

Java 使用正则表达式将字符串拆分为两个字符串,java,regex,string,split,Java,Regex,String,Split,这个问题以前被问过好几次,但我找不到答案: 我需要把一条线分成两条线。第一部分是日期,第二个字符串是文本。到目前为止,我得到的是: String test = "24.12.17 18:17 TestString"; String[] testSplit = test.split("\\d{2}.\\d{2}.\\d{2} \\d{2}:\\d{2}"); System.out.println(testSplit[0]); // "24.12.17 18:17" <--

这个问题以前被问过好几次,但我找不到答案: 我需要把一条线分成两条线。第一部分是日期,第二个字符串是文本。到目前为止,我得到的是:

String test = "24.12.17 18:17 TestString";
String[] testSplit = test.split("\\d{2}.\\d{2}.\\d{2} \\d{2}:\\d{2}");
System.out.println(testSplit[0]);           // "24.12.17 18:17" <-- Does not work
System.out.println(testSplit[1].trim());    // "TestString" <-- works
String test=“24.12.17 18:17 TestString”;
字符串[]testSplit=test.split(\\d{2}\\d{2}\\d{2}\\d{2}:\\d{2}”);

System.out.println(testSplit[0]);//“24.12.17 18:17”您只想匹配分隔符。通过匹配日期,您可以使用它(它被丢弃)

使用look behind,它断言但不使用:

test.split("(?<=^.{14}) ");

test.split((?如果您的字符串始终是这种格式(并且格式良好),您甚至不需要使用正则表达式。只需使用
.substring
.indexOf
在第二个空格处拆分即可:

String test = "24.12.17 18:17 TestString";
int idx = test.indexOf(" ", test.indexOf(" ") + 1);
System.out.println(test.substring(0, idx));
System.out.println(test.substring(idx).trim());

如果要确保字符串以日期时间值开头,可以使用匹配方法将字符串与包含两个捕获组的模式匹配:一个捕获日期,另一个捕获字符串的其余部分:

String test = "24.12.17 18:17 TestString";
String pat = "^(\\d{2}\\.\\d{2}\\.\\d{2} \\d{2}:\\d{2})\\s(.*)";
Matcher matcher = Pattern.compile(pat, Pattern.DOTALL).matcher(test);
if (matcher.find()) {
    System.out.println(matcher.group(1));
    System.out.println(matcher.group(2).trim());
}

详细信息

  • ^
    -字符串的开头
  • (\\d{2}\\.\\d{2}\\.\\d{2}\\d{2}:\\d{2})
    -第1组:日期时间模式(
    xx.xx.xx xx:xx
    -like模式)
  • \\s
    -一个空白(如果它是可选的,请在它后面添加
    *
  • (.*)
    -组2捕获到字符串末尾的任何0+字符(
    也将匹配换行符,因为
    模式.DOTALL
    标志)
跳过正则表达式;使用三个字符串 你工作太辛苦了。不需要把日期和时间都包括在一起。正则表达式很复杂,生命也很短暂

只需使用普通的三件,并重新组装日期时间

String[] pieces = "24.12.17 18:17 TestString".split( " " ) ;  // Split into 3 strings.
LocalDate ld = LocalDate.parse( pieces[0] , DateTimeFormatter.ofPattern( "dd.MM.uu" ) ) ;  // Parse the first string as a date value (`LocalDate`).
LocalTime lt = LocalTime.parse( pieces[1] , DateTimeFormatter.ofPattern( "HH:mm" ) ) ;  // Parse the second string as a time-of-day value (`LocalTime`).
LocalDateTime ldt = LocalDateTime.of( ld , lt ) ;  // Reassemble the date with the time (`LocalDateTime`).
String description = pieces[2] ;  // Use the last remaining string. 
看这个

ldt.toString():2017-12-24T18:17

描述:TestString


提示:如果您可以控制该输入,请切换到使用文本中日期时间值的标准格式。这些类在生成/解析字符串时默认使用标准格式。

字符串是否总是以该格式后跟任意文本的日期为准?如果您不必依赖日期&时间的长度是恒定的。为什么要投否决票?这正好回答了问题,而且效果很好。这实际上并没有回答问题,问题是“我需要把一个字符串分成两个字符串。”“@Bohemian它确实回答了这个问题。它确实将一个以特定模式开头的字符串分成两部分。如果你能证明相反,我将删除答案。“证明”是你的代码中没有
split
,也没有
string[]
结果。你可以创建一个
string[]
在代码中的某个地方,并为其元素赋值,但此时您最好编写
String[]testResult=new String[2];testResult[0]=test.substring(0,14);testResult[1]=test.substring(15)
这比你的答案要少很多代码,而且更简单,但仍然不能拆分字符串。请参阅我的答案,以获得一个简单、优雅的解决方案,它可以按要求拆分输入。@Bohemian:问题中不要求使用
split()
。意味着得到一个整体的一部分,我的代码就做到了。有几点可以证明我的解决方案比你的好:1)我的解决方案确保在字符串的开头有一个datetime,2)如果datetime和字符串的其余部分之间没有空格,我的解决方案会起作用,你的解决方案需要一个空格,3)即使有任何空格,我的解决方案也会起作用,而不仅仅是datetime和字符串其余部分之间的常规空格。
String[] pieces = "24.12.17 18:17 TestString".split( " " ) ;  // Split into 3 strings.
LocalDate ld = LocalDate.parse( pieces[0] , DateTimeFormatter.ofPattern( "dd.MM.uu" ) ) ;  // Parse the first string as a date value (`LocalDate`).
LocalTime lt = LocalTime.parse( pieces[1] , DateTimeFormatter.ofPattern( "HH:mm" ) ) ;  // Parse the second string as a time-of-day value (`LocalTime`).
LocalDateTime ldt = LocalDateTime.of( ld , lt ) ;  // Reassemble the date with the time (`LocalDateTime`).
String description = pieces[2] ;  // Use the last remaining string.