Skip to content

Instantly share code, notes, and snippets.

@deeTEEcee
Created July 23, 2019 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deeTEEcee/8ac579f4a44d3ca61bc95a99a996e36a to your computer and use it in GitHub Desktop.
Save deeTEEcee/8ac579f4a44d3ca61bc95a99a996e36a to your computer and use it in GitHub Desktop.
regex thoughts
/*
Regex is interesting because I found out the following:
* It's pretty easy to translate patterns into actual variables.
* Even if you have repeated patterns, those can easily go into variables. (e.g: `(<some_pattern>)+` can be translated into an infinite number of variables)
* Some languages have a multiline mode that allow you to apply regex line-by-line. That has the benefit
of giving you more easily readable options.
To see a better example, look at the following code.
Java double backslashes can be annoying but the IDE coloring helps with that a lot.
*/
public class RegexPractice {
@Test
public void testThis() {
System.out.println("test THIS start");
String testString = "# HELP jetty_requests_total Number of requests\n" +
"# TYPE jetty_requests_total counter\n" +
"jetty_requests_total 0.0\n" +
"# HELP jetty_requests_active Number of requests currently active\n" +
"# TYPE jetty_requests_active gauge\n" +
"jetty_requests_active 0.0\n" +
"jetty_responses_total{code=\"1xx\",test=\"true\",} 0.0\n";
Pattern regex = Pattern.compile("^(?!#)(\\w+)(\\{.*\\})? (\\d*\\.?\\d*)$", Pattern.MULTILINE);
Matcher regexMatcher = regex.matcher(testString);
while (regexMatcher.find()) {
String metric = regexMatcher.group(1);
String labels = regexMatcher.group(2);
if(labels != null) {
labels = labels.substring(1, labels.length() - 1); // Strip braces
Pattern labelPattern = Pattern.compile("(\\p{Alnum}+)=\"(\\p{Alnum}+)\",");
Matcher labelsMatcher = labelPattern.matcher(labels);
while(labelsMatcher.find()) {
System.out.println("label");
System.out.println(labelsMatcher.group(1));
System.out.println(labelsMatcher.group(2));
}
}
String value = regexMatcher.group(3);
System.out.println(metric);
System.out.println(labels);
System.out.println(value);
}
System.out.println("test THIS end");
}
@Test
public void testLabels() {
String labels = "code=\"hi\",blah=\"4xx\",";
Pattern labelPattern = Pattern.compile("(\\p{Alnum}+)=\"(\\p{Alnum}+)\",");
Matcher labelsMatcher = labelPattern.matcher(labels);
while(labelsMatcher.find()) {
System.out.println("label");
System.out.println(labelsMatcher.group(1));
System.out.println(labelsMatcher.group(2));
}
}
}
@deeTEEcee
Copy link
Author

Context: This was for regex parsing on prometheus but you really don't need it in this case. Just learned a few useful points while using it.

I try to avoid regex unless it's necessary to make things easier because well, new developers and things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment