Easy to trip with Java regular expressions


Here are some points to stumble upon with Java regular expressions.

I feel that people who often use regular expressions in other languages are addicted to it.

Escape character

Use the escape character \ when writing escape sequences.

However, if you want to express a "backslash" with a Java String type, you need to use one of the escape sequences, \\.

I wasted a whole day.

Code that throws a compile error

void check() {
    if (isFind("hoge.com")) {
        //Can't run

boolean isFind(String text) {
    //Compile error!!!!!
    Pattern p = Pattern.compile("\.com");
    return p.matcher(text).find();

Code that compiles

void check() {
    if (isFind("hoge.com")) {
        //Can be executed

boolean isFind(String text) {
    Pattern p = Pattern.compile("\\.com");
    return p.matcher(text).find();

The point is that if you want to represent the escape sequence \ . in other languages, you need to write \\ .. The wording around here is confusing, so please refer to the comment section.

Matcher # group (int) is not available immediately

The group (int) method of the Matcher class throws a run-time error if you don't use the find () method.

That's because the find () method checks for a string match. The group (int) method is just the getter of the string matched by the find () method.

Code that throws a run-time error

Pattern p = Pattern.compile("(A*).");

void print() {

String extract(String text) {
    Matcher m = p.matcher(text);
    //Run-time error!!!!!
    return m.group(1);

Code that can be executed properly

Pattern p = Pattern.compile("(A*).");

void print() {
    System.out.println(extract(AAABBB)); //output: AAA

String extract(String text) {
    Matcher m = p.matcher(text);
    if (m.find()) {
        return m.group(1);
    } else {
        return "";

in conclusion

Regular expressions are written in very different languages. If you think you're good at one language, it will hurt, so be careful.

~~ Perl is so easy! ~~

Recommended Posts

Easy to trip with Java regular expressions
Easy to make LINE BOT with Java Servlet
Easy BDD with (Java) Spectrum?
[Java] Summary of regular expressions
[Java] Introduction to lambda expressions
Connect to DB with Java
Connect to MySQL 8 with Java
Notes on how to use regular expressions in Java
Java lambda expressions learned with Comparator
Java to learn with ramen [Part 1]
[Java] Points to note with Arrays.asList ()
Parse Japanese addresses with regular expressions
I tried to interact with Java
Getting started with Java lambda expressions
Java, arrays to start with beginners
Easy database access with Java Sql2o
How to use Java lambda expressions
java regular expression-no loss to know-
Regular expressions
Replace with a value according to the match with a Java regular expression
How to compile Java with VsCode & Ant
Easy to make Slack Bot in Java
[Java] How to compare with equals method
Easily make troublesome regular expressions with Rubular
I tried to summarize Java lambda expressions
Introduction to algorithms with java --Search (depth-first search)
[Swift] Easy to implement modal with PanModal
Introduction to algorithms with java --Search (breadth-first search)
Challenge to deal with garbled characters with Java AudioSystem.getMixerInfo ()
[Java] How to test for null with JUnit
Switch beans with @ConditionalOnExpression and SpEL regular expressions
I tried to make Basic authentication with Java
Introduction to algorithms with java --Search (bit full search)
Deploy Java web app to Azure with maven
[Java] Summary of how to abbreviate lambda expressions
How to use Java framework with AWS Lambda! ??
Match IP addresses using regular expressions in Java
How to use Java API with lambda expression
Getting started with Kotlin to send to Java developers
Super easy way to use enum with JSP
[Java] Introduction to Java
Try to implement TCP / IP + NIO with JAVA
[Java] Article to add validation with Spring Boot 2.3.1.
Introduction to java
Where Java programmers tend to trip over Kotlin
I tried to break a block with java (1)
Easy to display hello world with Rails + Docker
Distinguish between integers and decimals with regular expressions
How to call functions in bulk with Java reflection
List processing to understand with pictures --java8 stream / javaslang-
Submit a job to AWS Batch with Java (Eclipse)
I tried to implement TCP / IP + BIO with JAVA
[Ruby] Exclude and replace specific patterns with regular expressions
[Java 11] I tried to execute Java without compiling with javac
[Java] How to omit spring constructor injection with Lombok
How to deploy Java to AWS Lambda with Serverless Framework
[Java Silver] Summary of points related to lambda expressions
[Java] How to encrypt with AES encryption with standard library
[Java] Refer to and set private variables with reflection
[Java] How to execute tasks on a regular basis
HTTPS connection with Java to the self-signed certificate server