[Java] Cut out a part of the character string with Matcher and regular expression

things to do

The URL string of Hatena Blog has a format such as "entry / year / month / day / time" (the URL in the example is invalid).

https://hogefugapiyo.hatenablog.com/entry/2019/09/02/000000

This time, as an example, the year, month, and day will be cut out from this URL.

manner

Use java.util.regex.Matcher.

Matcher matcher = Pattern
    .compile(".+/entry/(\\d+)/(\\d+)/(\\d+)/.+")
    .matcher(/*URL string to compare*/);

int year = Integer.parseInt(matcher.replaceFirst("$1"));
int month = Integer.parseInt(matcher.replaceFirst("$2"));
int day = Integer.parseInt(matcher.replaceFirst("$3"));

Commentary

In the title, it was expressed as cutout, but this process is originally intended to "if the input matches the regular expression, replace the matched part with the specified regular expression". In this case, the entire URL string to be compared matches the regular expression, so the entire string is replaced with the specified content, and as a result, it looks like it has been cut out.

What is $ 1? It's the regular expression enclosed in(). Within a regular expression this can be treated like a variable.

If you ignore the essence, replace . + / Entry / (\\ d +) / (\\ d +) / (\\ d +) /. + With `. +/ I think it is easy to understand.

Since it is a process for a character string, the result of cutting out will naturally be returned as a character string.

Another solution

The same processing can be done by writing as follows, but if you do it many times, it is more efficient to compile`` Pattern first.

String year = /*URL string to compare*/.replaceFirst(".+/entry/(\\d+)/(\\d+)/(\\d+)/.+", "$1");

bonus

Regular expressions can be improved by debugging them on the following sites (however, there are differences between languages, so you should also do unit tests).

Recommended Posts

[Java] Cut out a part of the character string with Matcher and regular expression
[Java] How to use substring to cut out a part of a character string
[Java] The confusing part of String and StringBuilder
<java> Split the address before and after the street address with a regular expression
[Java] How to cut out a character string character by character
<Java> Quiz to batch convert file names separated by a specific character string with a part of the file name
[Java] How to use substring to cut out a character string
String Replacement of the case where the regular expression * of the character string search condition contains a line break.
Replace with a value according to the match with a Java regular expression
Extract a part of a string with Ruby
[Ruby] Cut out a string using the slice method
[Java] Handling of character strings (String class and StringBuilder class)
Replace only part of the URL host with java
Extract elements by doing regular expression replacement from a lot of HTML with java
[Java] When putting a character string in the case of a switch statement, it is necessary to make it a constant expression
Cut out a Ruby string
Memorandum No.4 "Get a character string and decorate it" [Java]
[Java] Comparison method of character strings and comparison method using regular expressions
Set the date and time from the character string with POI
The nth and n + 1st characters of a Ruby string
The story of building a Java version of Minecraft server with GCP (and also set a whitelist)
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
A little regular expression story Part 1
Read the file under the classpath as a character string with spring
Connecting to a database with Java (Part 1) Maybe the basic method
A story about hitting the League Of Legends API with JAVA
A little regular expression story Part 2
I tried to summarize the methods of Java String and StringBuilder
Graph the sensor information of Raspberry Pi in Java and check it with a web browser
I want to extract between character strings with a regular expression
Generate a serial number with Hibernate (JPA) TableGenerator and store it in the Id of String.
A collection of phrases that impresses the "different feeling" of Java and JavaScript
Make a daily build of the TOPPERS kernel with Gitlab and Docker
The story of making a game launcher with automatic loading function [Java]
Invoke the character string passed as an argument as a method with send
Let's express the result of analyzing Java bytecode with a class diagram
[Java] Create a jar file with both compressed and uncompressed with the jar command
How to find out the Java version of a compiled class file
[Java] How to get to the front of a specific string using the String class
[Java] Check if the character string is composed only of blanks (= Blank)
[Java] Divide a character string by a specified character
Name a group of regular expressions (Java)
Split a string with ". (Dot)" in Java
[Java] Comparison of String type character strings
[Ruby] I want to make an array from a character string with the split method. And vice versa.
[Java] Difference between equals and == in a character string that is a reference type
[Java] How to easily get the longest character string of ArrayList using stream
Read the file under the classpath as a character string with spring
Validate the identity token of a user authenticated with AWS Cognito in Java
The story of low-level string comparison in Java
Read a string in a PDF file with Java
[Delete the first letter of the character string] Ruby
Come out with a suffix on the method
Measure the size of a folder in Java
[Java] Get the length of the surrogate pair string
[Note] Java: Measures the speed of string concatenation
I compared the characteristics of Java and .NET
JSON with Java and Jackson Part 2 XSS measures
Calculate the similarity score of strings with JAVA
Prepare a scraping environment with Docker and Java
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.