-1

I was hoping someone could help me with a pattern to split a string strictly on a character sequence of of three ^, i.e, ^^^

Input: Sample-1^^^Sample-2
Output: String 1: Sample-1 and String-2: Sample-2

I tried \\^\\^\\^ and it works fine for the happy path. But if I give it a string like:

Input: Sample-1^^^^Sample-2

I get the output as:

String 1: Sample-1
String-2: ^Sample-2

I tried the pattern (\\^\\^\\^) as well, but no luck.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
argo
  • 381
  • 1
  • 5
  • 15
  • 2
    I'm confused. You want to split it *stricly* one three `^^^`. Unless there's four? Then split it on four? – GBlodgett Aug 28 '18 at 19:01
  • It very much depends how your **overall** regex looks like. Please give a true [mcve]. Example code, and example input, and output. – GhostCat Aug 28 '18 at 19:06

1 Answers1

5

In this case you need \^+ (regex demo) which match one or more literal ^ character :

String[] output = input.split("\\^+");

Or if you want to match only 3 or 4 of literal ^ character you can use :

String[] output = input.split("\\^{3,4}");

Or if you want to match 3 or more of literal ^ character you can use :

String[] output = input.split("\\^{3,}");
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140