Skip to content

Instantly share code, notes, and snippets.

@DarkSeraphim
Last active August 29, 2015 14:05
Show Gist options
  • Save DarkSeraphim/48ab8271dc6da743e9f6 to your computer and use it in GitHub Desktop.
Save DarkSeraphim/48ab8271dc6da743e9f6 to your computer and use it in GitHub Desktop.
Regex galore
static String test = "0,&f&fAura Spell,4,28[&2Random Spellpart{2:-10},&fRandom Spellpart{4:7,3:5}]|1,&2&2Ball Spell,3,36[&fRandom Spellpart{1:9,3:-7}]|0,&f&fAura Spell,4,61[&5Random Spellpart{0:2,1:2},&fRandom Spellpart{1:-6}]";
static Pattern nullSplitter = Pattern.compile("\0");
static Pattern spellsPattern = Pattern.compile("((?:[0-9]+),(?:.*?),(?:[0-9]+),(?:[0-9]+)(?:\\[(?:.*?)\\]))\\|?");
static Pattern spellPattern = Pattern.compile("([0-9]+),(.*?),([0-9]+),([0-9]+)(?:\\[(.*?)\\])");
static Pattern spellPartsPattern = Pattern.compile("((?:.*?)\\{(?:.*?)\\}),?");
static Pattern spellPartPattern = Pattern.compile("(.*?)\\{(.*?)\\}");
static Pattern modifiersPattern = Pattern.compile("([0-9]+:-?[0-9]+),?");
static Pattern modifierPattern = Pattern.compile("([0-9]+):(-?[0-9]+)");
static int depth = 0;
static Matcher m;
public static void main(String[] args)
{
String[] spells = splitAndReturn(spellsPattern, test);
for(String spell : spells)
{
m = spellPattern.matcher(spell);
if(!m.matches())
continue;
print("name", m.group(2));
print("(attributes)", "");
depth++;
print("type", m.group(1));
print("rarity", m.group(3));
print("level", m.group(4));
print("(parts)", "");
depth++;
String[] parts = splitAndReturn(spellPartsPattern, m.group(5));
for(String part : parts)
{
m = spellPartPattern.matcher(part);
if(!m.matches())
continue;
print("name", m.group(1));
print("(modifiers)", "");
depth++;
String[] modifiers = splitAndReturn(modifiersPattern, m.group(2));
for(String modifier : modifiers)
{
m = modifierPattern.matcher(modifier);
if(!m.matches())
continue;
print("type", m.group(1));
print("power", m.group(2));
}
depth--;
}
depth--;
depth--;
}
}
private static String[] splitAndReturn(Pattern pat, String src)
{
m = pat.matcher(src);
if(!m.matches())
return new String[0];
return nullSplitter.split(m.replaceAll("$1\0"));
}
private static void print(String key, String value)
{
String p = "";
for(int i = 0; i < depth * 4; i++)
p += " ";
p += key + ": " + value;
System.out.println(p);
}
// With split instead
public static void main(String[] args)
{
long total = 0;
for(int i = 0; i < 1000000; i++)
{
long start = System.nanoTime();
String[] spells = test.split("\\|");
for (String spell : spells) // Spells
{
String[] spell0 = spell.split(",", 5);
spell0[0] // SpellType
spell0[1] // Name
spell0[2] // Rarity
spell0[3] // Level
spell0[4] = spell0[4].substring(1,spell0[4].length()-2);
for (String part : spell0[4].split("\\},")) // SpellParts
{
String[] part0 = part.split("\\{");
part0[1] // SpellPart name
String[] modifiers = part0[1].split(",");
for (String modifier : modifiers) // Modifiers
{
String[] mod = modifier.split(":");
mod[0] // ModifierType
mod[1] // Modifier value
}
}
}
total += (System.nanoTime() - start);
}
System.out.println(total / 1000000); // Result in ns
System.out.println(total / 1000000 / 1000000); // Result in ms (for initial tick based comparison)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment