Skip to content

Instantly share code, notes, and snippets.

@luketn
Created March 28, 2019 03:57
Show Gist options
  • Save luketn/f726437678dda0dae97862b287e3920a to your computer and use it in GitHub Desktop.
Save luketn/f726437678dda0dae97862b287e3920a to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* This regex matches JSON documents with the properties 'name' and 'type' and returns a list of the 'dir' type documents.
* e.g. [0.0.1-pre-alpha, 0.0.2-pre-alpha, 0.0.3-pre-alpha, 0.0.4-pre-alpha]
*/
public class RegExFun {
private static List<String> getDirectories(String githubContents) {
List<String> directories = new ArrayList<>();
Pattern pattern = Pattern.compile("\\{.*?\"name\": \"(.*?)\".*?\"type\": \"(.*?)\".*?\\}", Pattern.DOTALL);
Matcher matcher = pattern.matcher(githubContents);
while (matcher.find()) {
String name = matcher.group(1);
String type = matcher.group(2);
if (type.equals("dir")) {
directories.add(name);
}
}
return directories;
}
public static void main(String[] args) {
System.out.println(getDirectories("[\n" +
" {\n" +
" \"name\": \"0.0.1-pre-alpha\",\n" +
" \"path\": \"0.0.1-pre-alpha\",\n" +
" \"sha\": \"cc075baba4e2b9207be846d9d152bf3d6f73295a\",\n" +
" \"size\": 0,\n" +
" \"url\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/contents/0.0.1-pre-alpha?ref=master\",\n" +
" \"html_url\": \"https://github.com/Jett59/totallyAccurateExplosionSimulator/tree/master/0.0.1-pre-alpha\",\n" +
" \"git_url\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/git/trees/cc075baba4e2b9207be846d9d152bf3d6f73295a\",\n" +
" \"download_url\": null,\n" +
" \"type\": \"dir\",\n" +
" \"_links\": {\n" +
" \"self\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/contents/0.0.1-pre-alpha?ref=master\",\n" +
" \"git\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/git/trees/cc075baba4e2b9207be846d9d152bf3d6f73295a\",\n" +
" \"html\": \"https://github.com/Jett59/totallyAccurateExplosionSimulator/tree/master/0.0.1-pre-alpha\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"name\": \"0.0.2-pre-alpha\",\n" +
" \"path\": \"0.0.2-pre-alpha\",\n" +
" \"sha\": \"24496e0e9a1644238af293f5b93678752cc7d699\",\n" +
" \"size\": 0,\n" +
" \"url\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/contents/0.0.2-pre-alpha?ref=master\",\n" +
" \"html_url\": \"https://github.com/Jett59/totallyAccurateExplosionSimulator/tree/master/0.0.2-pre-alpha\",\n" +
" \"git_url\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/git/trees/24496e0e9a1644238af293f5b93678752cc7d699\",\n" +
" \"download_url\": null,\n" +
" \"type\": \"dir\",\n" +
" \"_links\": {\n" +
" \"self\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/contents/0.0.2-pre-alpha?ref=master\",\n" +
" \"git\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/git/trees/24496e0e9a1644238af293f5b93678752cc7d699\",\n" +
" \"html\": \"https://github.com/Jett59/totallyAccurateExplosionSimulator/tree/master/0.0.2-pre-alpha\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"name\": \"0.0.3-pre-alpha\",\n" +
" \"path\": \"0.0.3-pre-alpha\",\n" +
" \"sha\": \"a104e691260b4e9db93cc53887222780b7986add\",\n" +
" \"size\": 0,\n" +
" \"url\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/contents/0.0.3-pre-alpha?ref=master\",\n" +
" \"html_url\": \"https://github.com/Jett59/totallyAccurateExplosionSimulator/tree/master/0.0.3-pre-alpha\",\n" +
" \"git_url\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/git/trees/a104e691260b4e9db93cc53887222780b7986add\",\n" +
" \"download_url\": null,\n" +
" \"type\": \"dir\",\n" +
" \"_links\": {\n" +
" \"self\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/contents/0.0.3-pre-alpha?ref=master\",\n" +
" \"git\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/git/trees/a104e691260b4e9db93cc53887222780b7986add\",\n" +
" \"html\": \"https://github.com/Jett59/totallyAccurateExplosionSimulator/tree/master/0.0.3-pre-alpha\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"name\": \"0.0.4-pre-alpha.zip\",\n" +
" \"path\": \"0.0.4-pre-alpha.zip\",\n" +
" \"sha\": \"6c7c2ae176b8642b85db96bcfc969bd60dafb28f\",\n" +
" \"size\": 54119,\n" +
" \"url\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/contents/0.0.4-pre-alpha.zip?ref=master\",\n" +
" \"html_url\": \"https://github.com/Jett59/totallyAccurateExplosionSimulator/blob/master/0.0.4-pre-alpha.zip\",\n" +
" \"git_url\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/git/blobs/6c7c2ae176b8642b85db96bcfc969bd60dafb28f\",\n" +
" \"download_url\": \"https://raw.githubusercontent.com/Jett59/totallyAccurateExplosionSimulator/master/0.0.4-pre-alpha.zip\",\n" +
" \"type\": \"file\",\n" +
" \"_links\": {\n" +
" \"self\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/contents/0.0.4-pre-alpha.zip?ref=master\",\n" +
" \"git\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/git/blobs/6c7c2ae176b8642b85db96bcfc969bd60dafb28f\",\n" +
" \"html\": \"https://github.com/Jett59/totallyAccurateExplosionSimulator/blob/master/0.0.4-pre-alpha.zip\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"name\": \"0.0.4-pre-alpha\",\n" +
" \"path\": \"0.0.4-pre-alpha\",\n" +
" \"sha\": \"b384f4d0272b358d4794dda5c2c16480be2f11c7\",\n" +
" \"size\": 0,\n" +
" \"url\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/contents/0.0.4-pre-alpha?ref=master\",\n" +
" \"html_url\": \"https://github.com/Jett59/totallyAccurateExplosionSimulator/tree/master/0.0.4-pre-alpha\",\n" +
" \"git_url\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/git/trees/b384f4d0272b358d4794dda5c2c16480be2f11c7\",\n" +
" \"download_url\": null,\n" +
" \"type\": \"dir\",\n" +
" \"_links\": {\n" +
" \"self\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/contents/0.0.4-pre-alpha?ref=master\",\n" +
" \"git\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/git/trees/b384f4d0272b358d4794dda5c2c16480be2f11c7\",\n" +
" \"html\": \"https://github.com/Jett59/totallyAccurateExplosionSimulator/tree/master/0.0.4-pre-alpha\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"name\": \"Missile Launcher.jar\",\n" +
" \"path\": \"Missile Launcher.jar\",\n" +
" \"sha\": \"7f13a3a33ce5607ac511f0242e4355b936a243be\",\n" +
" \"size\": 4399,\n" +
" \"url\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/contents/Missile%20Launcher.jar?ref=master\",\n" +
" \"html_url\": \"https://github.com/Jett59/totallyAccurateExplosionSimulator/blob/master/Missile%20Launcher.jar\",\n" +
" \"git_url\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/git/blobs/7f13a3a33ce5607ac511f0242e4355b936a243be\",\n" +
" \"download_url\": \"https://raw.githubusercontent.com/Jett59/totallyAccurateExplosionSimulator/master/Missile%20Launcher.jar\",\n" +
" \"type\": \"file\",\n" +
" \"_links\": {\n" +
" \"self\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/contents/Missile%20Launcher.jar?ref=master\",\n" +
" \"git\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/git/blobs/7f13a3a33ce5607ac511f0242e4355b936a243be\",\n" +
" \"html\": \"https://github.com/Jett59/totallyAccurateExplosionSimulator/blob/master/Missile%20Launcher.jar\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"name\": \"download.html\",\n" +
" \"path\": \"download.html\",\n" +
" \"sha\": \"da8bf5f755589885e441b7ab39588266b7037ec2\",\n" +
" \"size\": 1101,\n" +
" \"url\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/contents/download.html?ref=master\",\n" +
" \"html_url\": \"https://github.com/Jett59/totallyAccurateExplosionSimulator/blob/master/download.html\",\n" +
" \"git_url\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/git/blobs/da8bf5f755589885e441b7ab39588266b7037ec2\",\n" +
" \"download_url\": \"https://raw.githubusercontent.com/Jett59/totallyAccurateExplosionSimulator/master/download.html\",\n" +
" \"type\": \"file\",\n" +
" \"_links\": {\n" +
" \"self\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/contents/download.html?ref=master\",\n" +
" \"git\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/git/blobs/da8bf5f755589885e441b7ab39588266b7037ec2\",\n" +
" \"html\": \"https://github.com/Jett59/totallyAccurateExplosionSimulator/blob/master/download.html\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"name\": \"explosion sound.aif\",\n" +
" \"path\": \"explosion sound.aif\",\n" +
" \"sha\": \"6347e1b3fba5032943d0167372b3f5eb9d5d1d62\",\n" +
" \"size\": 140044,\n" +
" \"url\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/contents/explosion%20sound.aif?ref=master\",\n" +
" \"html_url\": \"https://github.com/Jett59/totallyAccurateExplosionSimulator/blob/master/explosion%20sound.aif\",\n" +
" \"git_url\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/git/blobs/6347e1b3fba5032943d0167372b3f5eb9d5d1d62\",\n" +
" \"download_url\": \"https://raw.githubusercontent.com/Jett59/totallyAccurateExplosionSimulator/master/explosion%20sound.aif\",\n" +
" \"type\": \"file\",\n" +
" \"_links\": {\n" +
" \"self\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/contents/explosion%20sound.aif?ref=master\",\n" +
" \"git\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/git/blobs/6347e1b3fba5032943d0167372b3f5eb9d5d1d62\",\n" +
" \"html\": \"https://github.com/Jett59/totallyAccurateExplosionSimulator/blob/master/explosion%20sound.aif\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"name\": \"index.html\",\n" +
" \"path\": \"index.html\",\n" +
" \"sha\": \"86add7f35e063ec06b4b68001a80e624df708e43\",\n" +
" \"size\": 284,\n" +
" \"url\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/contents/index.html?ref=master\",\n" +
" \"html_url\": \"https://github.com/Jett59/totallyAccurateExplosionSimulator/blob/master/index.html\",\n" +
" \"git_url\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/git/blobs/86add7f35e063ec06b4b68001a80e624df708e43\",\n" +
" \"download_url\": \"https://raw.githubusercontent.com/Jett59/totallyAccurateExplosionSimulator/master/index.html\",\n" +
" \"type\": \"file\",\n" +
" \"_links\": {\n" +
" \"self\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/contents/index.html?ref=master\",\n" +
" \"git\": \"https://api.github.com/repos/Jett59/totallyAccurateExplosionSimulator/git/blobs/86add7f35e063ec06b4b68001a80e624df708e43\",\n" +
" \"html\": \"https://github.com/Jett59/totallyAccurateExplosionSimulator/blob/master/index.html\"\n" +
" }\n" +
" }\n" +
"]\n"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment