简体   繁体   English

使用多个定界符分割不起作用

[英]Split with multiple delimiters not working

For some reason my multi delimiter split is not working. 由于某种原因,我的多定界符拆分不起作用。 Hope it just a syntax error. 希望它只是语法错误。

This works, but I want to also split if it finds end date 这可行,但是如果它找到结束日期,我也想拆分

String dateList[] = test.split("(?="+StartDate+")"); 

But this does not. 但这不是。 Am I missing something? 我想念什么吗?

String dateList[] = text.split("[(?="+StartDate+")(?="+EndDate+")]");

You cannot use "lookarounds" in a custom character class - they'd be just interpreted as characters of the class (and may not even compile the pattern properly if a malformed range is detected, eg with dangling - characters). 你不能在自定义字符类中使用“lookarounds” -他们会仅仅解释为类的字符(甚至可能不与悬挂正确编译模式如果检测到畸形的范围,例如-字符)。

Use the | 使用| operator to alternate between StartDate and EndDate . 运算符,以在StartDateEndDate之间切换。

Something like: 就像是:

String dateList[] = text.split("(?="+StartDate+"|"+EndDate+")");

Notes 笔记

  • You also may want to invoke Pattern.quote on your start and end date values, in case they contain reserved characters. 如果开始日期和结束日期值包含保留字符,则可能还需要调用Pattern.quote
  • Java variable naming convention is camelBack , not CamelCase Java变量命名约定是camelBack ,而不是CamelCase

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM