Matcher matcher = new Matcher(this, input); ArrayList list = new ArrayList(); int empties = 0; int count = 0; int start = 0; int end; boolean matched;  while (matched = matcher.find() && (limit <= 0 || count < limit - 1)) { ++count; end = matcher.start(); if (start == end) empties++; else { while (empties-- > 0) list.add("");  String text = input.subSequence(start, end).toString(); list.add(text); } start = matcher.end(); }   if (!matched && count == 0) return new String[] { input.toString() };   boolean emptyLast = (start == input.length());   if (list.size() < limit || limit < 0 || (limit == 0 && !emptyLast)) { if (limit > list.size()) { int max = limit - list.size(); empties = (empties > max) ? max : empties; } while (empties-- > 0) list.add(""); }   if (limit != 0 || (limit == 0 && !emptyLast)) { String t = input.subSequence(start, input.length()).toString(); if ("".equals(t) && limit == 0) ; else list.add(t); }  String[] output = new String [list.size()]; list.toArray(output); return output;
return split(input, 0);
  public String[] split (CharSequence input, int limit)  {    Matcher matcher = new Matcher(this, input);    ArrayList list = new ArrayList();    int empties = 0;    int count = 0;    int start = 0;    int end;    boolean matched;    while (matched = matcher.find() && (limit <= 0 || count < limit - 1))      {	++count;	end = matcher.start();	if (start == end)	  empties++;	else	  {	    while (empties-- > 0)	      list.add("");	    String text = input.subSequence(start, end).toString();	    list.add(text);	  }	start = matcher.end();      }    // We matched nothing.    if (!matched && count == 0)      return new String[] { input.toString() };        // Is the last token empty?    boolean emptyLast = (start == input.length());    // Can/Must we add empties or an extra last token at the end?    if (list.size() < limit || limit < 0 || (limit == 0 && !emptyLast))      {	if (limit > list.size())	  {	    int max = limit - list.size();	    empties = (empties > max) ? max : empties;	  }	while (empties-- > 0)	  list.add("");      }    // last token at end    if (limit != 0 || (limit == 0 && !emptyLast))      {	String t = input.subSequence(start, input.length()).toString();	if ("".equals(t) && limit == 0)	  ; // Don't add.	else	  list.add(t);      }    String[] output = new String [list.size()];    list.toArray(output);    return output;  }