Name a group of regular expressions (Java)

wrap up

background

In order to make the parts versatile, regular expressions may be sent out to property files.

For example, if you want to extract a log of the form time [thread name] error level class name --message for each element, you can write as follows.

log.pattern=(.+?) \[(.+?)\](ERROR|INFO|DEBUG) (.*?) - (.*?)
String log = "22:22:50.324 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : []";

String patternString = getProperty("log.pattern");
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(log);
if (matcher.matches()) {
	String time      = matcher.group(1);
	String thread    = matcher.group(2);
	String level     = matcher.group(3);
	String className = matcher.group(4);
	String message   = matcher.group(5);

・ ・ ・
} 

At first glance it looks versatile, but this technique has major weaknesses. That is, the order of the groups cannot be changed. This is because the Java code specifies the order of the groups to retrieve the values of the groups.

For example, the code breaks as soon as the thread name and error level are swapped as shown below. I intended to make it general purpose.

22:22:50.324 DEBUG [main]  org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : [] 

Regular expression group with named forward references

Starting with Java 1.7, "Regular expression groups with named forward references" are available.

You can name a group of regular expressions by writing (? <Name> pattern).

Let's apply it to the regular expression.

log.pattern=(?<time>.+?) \[(?<thread>.+?)\](?<level>ERROR|INFO|DEBUG) (?<className>.*?) - (?<message>.*?)

To retrieve this, use Matcher # group (String name)

String time      = matcher.group("time");
String thread    = matcher.group("thread");
String level     = matcher.group("level");
String className = matcher.group("className");
String message   = matcher.group("message");

Since it is acquired by name, even if the thread name and error level order are changed, it can be handled by simply changing the regular expression.

log.pattern=(?<time>.+?) (?<level>ERROR|INFO|DEBUG) \[(?<thread>.+?)\](?<className>.*?) - (?<message>.*?)

It's a simple but convenient function.

reference

Recommended Posts

Name a group of regular expressions (Java)
[Java] Summary of regular expressions
[Java] Comparison method of character strings and comparison method using regular expressions
Think of a Java update strategy
Sort a List of Java objects
Implementation of validation using regular expressions
A brief description of JAVA dependencies
The origin of Java lambda expressions
A little understanding of lambda expressions
Regular expressions
[Java] Let's take a look at Switch Expressions (Preview) of JDK 13.
[Java] Returns a Japanese name file in filename of HTTP header
Regular expressions that match 99% of email addresses
Easy to trip with Java regular expressions
Do you need a memory-aware implementation of Java?
[Java] Summary of how to abbreviate lambda expressions
Measure the size of a folder in Java
Match IP addresses using regular expressions in Java
Method name of static factory method learned from Java 8
A collection of simple questions for Java beginners
A quick review of Java learned in class
Method name of method chain in Java Builder + α
[Java] Overview of Java
[Java] Cut out a part of the character string with Matcher and regular expression
Extract elements by doing regular expression replacement from a lot of HTML with java
Get a list of MBean information for Java applications
A quick review of Java learned in class part4
[Java Silver] Summary of points related to lambda expressions
Use Java lambda expressions outside of the Stream API
Awesome Java: A number of great Java framework library software
Experience of passing Java Silver as a new graduate
Let's make a robot! "A simple demo of Java AWT Robot"
[Java] How to execute tasks on a regular basis
[Java] When writing the source ... A memorandum of understanding ①
A quick review of Java learned in class part3
A quick review of Java learned in class part2
A survey of the Kubernetes native Java framework Quarkus
Notes on how to use regular expressions in Java
A group of friends who never allow double submission
Predicted Features of Java
[Java] Significance of serialVersionUID
[Java] Create a filter
NIO.2 review of java
Review of java Shilber
java --Unification of comments
Understand Java 8 lambda expressions
java build a triangle
Notes on regular expressions
History of Java annotation
java (merits of polymorphism)
About Java lambda expressions
Explain Java 8 lambda expressions
colorize and regular expressions
NIO review of java
java regular expression summary
[Java] Three features of Java
Summary of Java support 2018
Story of making a task management application with swing, java
A quick explanation of the five types of static in Java
Activate Excel file A1 cell of each sheet in Java
Implementation of a math parser with recursive descent parsing (Java)