TJF
TJF

Reputation: 45

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+")]");

Upvotes: 1

Views: 44

Answers (1)

Mena
Mena

Reputation: 48444

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, e.g. with dangling - characters).

Use the | operator to alternate between StartDate and EndDate.

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.
  • Java variable naming convention is camelBack, not CamelCase

Upvotes: 2

Related Questions