Skip to content

Instantly share code, notes, and snippets.

@JerrettDavis
Created June 6, 2014 16:29
Show Gist options
  • Select an option

  • Save JerrettDavis/f9173949fc18022872fa to your computer and use it in GitHub Desktop.

Select an option

Save JerrettDavis/f9173949fc18022872fa to your computer and use it in GitHub Desktop.
import java.util.Arrays;
import java.util.regex.*;
public class Parser {
public Parser() {
}
public static String[] getValues(String dson, String property) {
String re1 = "(such)(\\s+)(\"" + property
+ "\")(\\s+)(is)(\\s+)(so)(\\s+)(.*?)(\\s+)(many)(\\s+)(wow)";
Pattern p = Pattern.compile(re1, Pattern.CASE_INSENSITIVE
| Pattern.DOTALL);
Matcher m = p.matcher(dson);
if (m.find()) {
String temp[] = conditionString(m.group(9)).split(" next ");
return temp;
} else {
System.out.println(dson);
return null;
}
}
public static String getValue(String dson, String property) {
String re1 = "(such)(\\s+)(\"" + property
+ "\")(\\s+)(is)(\\s+)((\\S+?))(\\s+)(wow)";
Pattern p = Pattern.compile(re1, Pattern.CASE_INSENSITIVE
| Pattern.DOTALL);
Matcher m = p.matcher(dson);
if (m.find()) {
return conditionString(m.group(7));
} else {
System.out.println(re1);
return null;
}
}
public static String conditionString(String data) {
String temp = removeVery(data);
temp = temp.replace("notfalse", "true")
.replace("nottrue", "false")
.replace("nullish", "null")
.replace("\"", "");
return temp;
}
private static String removeVery(String data) {
String reg = "(\\d+(?:\\.\\d+)?)(very)(\\d+)";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(data);
StringBuffer sb = new StringBuffer();
while (m.find()) {
String rep = m.group(1) + m.group(2).replaceAll("very", "e")
+ m.group(3);
m.appendReplacement(sb, rep);
}
m.appendTail(sb);
return sb.toString();
}
public static void main(String[] args0) {
String txt = "such \"foo\" is \"bar\" wow";
String txt2 = "such \"foo\" is so \"bar\" next \"baz\" next \"fizzbuzz\" many wow";
String txt3 = "such \"foo\" is 42very3 wow ";
String temp = Parser.getValue(txt, "foo");
String temp2[] = Parser.getValues(txt2, "foo");
String temp3 = Parser.getValue(txt3, "foo");
System.out.println("foo = " + temp);
System.out.println("foo = " + Arrays.toString(temp2));
System.out.println("foo = " + Double.parseDouble(temp3));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment