Created
February 18, 2017 03:56
-
-
Save Rafael09ED/bae7a4d7ccda33c3eae0043103f6ae14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.rafael09ed.nMMModProfileExporter; | |
import java.util.Arrays; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
/** | |
* @author Rafael | |
* @version 1.0 2/17/2017 | |
*/ | |
public class SplitAroundRegex { | |
private static List<String> splitAroundRegex(String toSplit, String regex) { | |
List<String> list = new LinkedList<>(); | |
String[] parts = toSplit.split(regex); | |
Matcher matcher = Pattern.compile(regex).matcher(toSplit); | |
int i = 0, l = 0; | |
while (matcher.find()) { | |
l += parts[i].length() + matcher.group().length(); | |
list.add(parts[i++]); | |
list.add(matcher.group()); | |
} | |
if (toSplit.length() != l) | |
list.add(parts[i]); | |
return list; | |
} | |
private static String[] splitAroundRegex2(String toSplit, String regex) { | |
String[] parts = toSplit.split(regex); | |
String[] list = new String[parts.length * 2]; | |
Matcher matcher = Pattern.compile(regex).matcher(toSplit); | |
int i = -1; | |
while (++i * 2 < list.length) { | |
list[i * 2] = parts[i]; | |
if (matcher.find()) | |
list[i * 2 + 1] = matcher.group(0); | |
} | |
return list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment