[Java] Summary of regular expressions

List

pattern Match string
XYZ The string XYZ
[XYZ] One character of XYZ
[^XYZ] One character other than XYZ
[X-Z] X~One letter in the range of Y
[a-zA-Z] Range from a to z or A to Z
(X|Y|Z) One of XYZ
[ABC|]
X* X occurs 0 or more times
("do*n"in the case of"dn","don","dooon"Such)
X+ X occurs more than once
("do+n"in the case of"don","dooon"Such)
X? X occurs 0 or 1 times
("do?n"in the case of"dn"Or"don")
X{n} X occurs n times
("do{2}n"in the case of"doon")
X{n,} X occurs n times or more
("do{2,}n"in the case of"doon","doooon"Such)
X{n,m} X occurs n to m times
("do{2,3}n"in the case of"doon"Or"dooon")
. Any character
\w uppercase letter/Lowercase alphanumeric characters, underscore
[a-zA-Z_0-9]
\d Numbers
[0-9]
\D Other than numbers
[^0-9]
\s Blank
[ \t\n\x0B\f\r]
^ Match at the beginning of a line
$ Line tail match
\b Word boundaries
\\ Match backslash
\n Match newline character
\t Match tab character
^\d{10}$ 10 single-byte numbers
^\d{5,10}$ Half-width number 5 digits or more and 10 digits or less
\d{2,4}-\d{2,4}-\d{4} phone number
(Half-width number 2~3 digits-Half-width number 2~3 digits-4 single-byte numbers)
^\d{3}-\d{4}$ Postal code
(3 single-byte numbers-4 single-byte numbers)
[1]+$ One-digit or more half-width alphanumeric characters
(0-9、a-z、A-Z)

Phone / Zip Code Matching Sample

//Sample 1 Phone/Zip code match
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
 public static void main(String[] args) {
    //Character string to judge
    String str = "012-345-6789";
    //Generate a pattern to judge
    Pattern p = Pattern.compile("\\d{2,4}-\\d{2,4}-\\d{4}"); //phone number
    //Pattern p = Pattern.compile("^\\d{3}-\\d{4}$"); //Postal code
    Matcher m = p.matcher(str);

    System.out.println(m.find()); //true
    }
}

Forward / backward / partial match sample

//Sample 2 forward/Rear/Partial Match
public class Main {
 public static void main(String[] args) {
    //Character string to judge
    String str = "000012-345-6789";
    //Generate a pattern to judge
    //Pattern p = Pattern.compile("\\d{2,4}-\\d{2,4}-\\d{4}.*"); //Phone number prefix match
    //Pattern p = Pattern.compile(".*\\d{2,4}-\\d{2,4}-\\d{4}"); //Phone number suffix match
    Pattern p = Pattern.compile(".*\\d{2,4}-\\d{2,4}-\\d{4}.*"); //Phone number partial match
    Matcher m = p.matcher(str);

    System.out.println(m.find()); //true
    }
}

Extract the matched string

import java.util.*;
import java.util.regex.Pattern;

public class Main {
  public static void main(String[] args) {
    var str = "Mobile is 0123-99-It's 0000. Home is 000-123-4567 and so on.";
    var ptn = Pattern.compile("(\\d{2,4})-(\\d{2,4})-(\\d{4})");
    var match = ptn.matcher(str);
    while (match.find()) {
      System.out.println("Starting position:" + match.start());
      System.out.println("End position:" + match.end());
      System.out.println("Matching string:" + match.group());
      System.out.println("Area code:" + match.group(1));
      System.out.println("City code:" + match.group(2));
      System.out.println("Subscriber number:" + match.group(3));
      System.out.println("-----");
    }
  }
}

Behavior control during matching

CASE_INSENSITIVE

import java.util.*;
import java.util.regex.Pattern;

public class Main {
  public static void main(String[] args) {
    var str = "For work is [email protected] is com. NEKO for private [email protected] is com.";
    var ptn = Pattern.compile("[a-z0-9.!#$%&'*+/=?^_{|}~-]+@[a-z0-9-]+(\\.[a-z0-9-]+)*", Pattern.CASE_INSENSITIVE);
    var match = ptn.matcher(str);
    while (match.find()) {
      System.out.println(match.group());
    }
  }
}

MULTILINE

import java.util.*;
import java.util.regex.Pattern;

public class Main {
  public static void main(String[] args) {
    var str = "Friends in the first grade\I wonder if n100 people can do it\n";
    // var ptn = Pattern.compile("^\\d*");
    var ptn = Pattern.compile("^\\d*", Pattern.MULTILINE);
    var match = ptn.matcher(str);
    while (match.find()) {
      System.out.println(match.group()); //1 100
    }
  }
}

DOTALL

import java.util.*;
import java.util.regex.Pattern;

public class Main {
  public static void main(String[] args) {
    var str = "Wanted to meet\nWanted to meet\nWanted to meet\nYES";
    // var ptn = Pattern.compile("^.+");
    var ptn = Pattern.compile("^.+", Pattern.DOTALL);
    var match = ptn.matcher(str);
    while (match.find()) {
      System.out.println(match.group());
      //Wanted to meet
      //Wanted to meet
      //Wanted to meet
      //YES
    }
  }
}

Embedded flag

var ptn = Pattern.compile("(?i)[a-z0-9.!#$%&'*+/=?^_{|}~-]+@[a-z0-9-]+(\\.[a-z0-9-]+)*");

//var ptn = Pattern.compile("[a-z0-9.!#$%&'*+/=?^_{|}~-]+@[a-z0-9-]+(\\.[a-z0-9-]+)*", Pattern.CASE_INSENSITIVE);

Longest match / shortest match

import java.util.regex.Pattern;

public class Main {
  public static void main(String[] args) {
    var tags = "<p><strong>NEKO</strong>site<a href='index.html'><img src='cat.jpg' /></a></p>";
    //Longest match
    //var ptn = Pattern.compile("<.+>"); //<p><strong>NEKO</strong>site<a href='index.html'><img src='cat.jpg' /></a></p>
    //Shortest match
    var ptn = Pattern.compile("<.+?>");
    var match = ptn.matcher(tags);
    while (match.find()) {
      System.out.println(match.group());
      //<p>
      //<strong>
      //</strong>
      //<a href='index.html'>
      //<img src='cat.jpg' />
      //</a>
      //</p>
    }
  }
}

Named capture

import java.util.regex.Pattern;

public class Main {
  public static void main(String[] args) {
    var msg = "Mobile is 0123-99-It's 0000. Home is 000-123-4567 and so on.";
    var ptn = Pattern.compile("(?<area>\\d{2,4})-(?<city>\\d{2,4})-(?<local>\\d{4})");
    var match = ptn.matcher(msg);
    while (match.find()) {
      System.out.println("Starting position:" + match.start());
      System.out.println("End position:" + match.end());
      System.out.println("Matching string:" + match.group());
      System.out.println("Area code:" + match.group("area"));
      System.out.println("City code:" + match.group("city"));
      System.out.println("Subscriber number:" + match.group("local"));
      System.out.println("-----");
    }
  }
}

Back reference

import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
  //Back reference
  public static void main(String args[]){
    String str1 = "My name is <div>Neko</div>";
    String str2 = "I am a <span>Cat</span>";
    String str3 = "<span>Hello World</div>";
    String regex = "<(div|span)>.*?<\\/\\1>";
    Pattern p = Pattern.compile(regex);
    System.out.println("pattern: " + regex);
    check(p, str1);
    check(p, str2);
    check(p, str3);
  }
  private static void check(Pattern p, String target){
    Matcher m = p.matcher(target);

    if (m.find()){
      System.out.println("Match! " + target);
    }else{
      System.out.println("Unmatch! " + target);
    }
  }
}

Look-ahead / look-ahead

import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {

  private static void match(Pattern ptn, String input) {
    var match = ptn.matcher(input);
    while (match.find()) {
      System.out.println(match.group());
    }
    System.out.println("---");
  }

  public static void main(String[] args) {
    var re1 = Pattern.compile("My(?=Yes)"); 
    var re2 = Pattern.compile("My(?!Yes)"); 
    var re3 = Pattern.compile("(?<=。)My");  
    var re4 = Pattern.compile("(?<!。)My"); 
    var msg1 = "My yes is a cat";        
    var msg2 = "I am a cat. My name isn't there yet.";
    match(re1, msg1); //My
    match(re1, msg2); //---
    match(re2, msg1); //---
    match(re2, msg2); //My,My,My
    match(re3, msg1); //---
    match(re3, msg2); //My
    match(re4, msg1); //My
    match(re4, msg2); //My,My
  }
}

String replacement

import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {

  public static void main(String[] args) {
    var str = "Click here for inquiries https://www.neko.com/is.";
    System.out.println(str.replaceAll(
        "(?i)http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\\\w ./?%&=-]*)?",
        "<a href=\"$0\">$0</a>"));
     //Contact Us<a href="https://www.neko.com/">https://www.neko.com/</a>is.
  }
}

String split

import java.util.regex.Pattern;
public class Main {
  //One or more digits+Split with
  public static void main(String[] args) {
    var str = "There are 2 chickens in the backyard and 22 chickens in the backyard.";
    var re = Pattern.compile("\\d{1,}Wow");
    var result = re.split(str);
    System.out.println(String.join(" ", result)); 
    //There is a chicken in the backyard
  }
}

  1. 0-9a-zA-Z ↩︎

Recommended Posts

[Java] Summary of regular expressions
Name a group of regular expressions (Java)
java regular expression summary
Summary of Java support 2018
[Java] Summary of how to abbreviate lambda expressions
[Java11] Stream Summary -Advantages of Stream-
[Java] Summary of operators (operator)
Summary of Java language basics
[Java] Summary of for statements
Summary of Java Math class
[Java] Summary of control syntax
Summary of java error processing
[Java] Summary of design patterns
[Java] Summary of mathematical operations
Regular expressions
[Java Silver] Summary of points related to lambda expressions
[For beginners] Summary of java constructor
Summary of [Java silver study] package
Implementation of validation using regular expressions
Summary of object-oriented programming using Java
The origin of Java lambda expressions
[Java] Comparison method of character strings and comparison method using regular expressions
Summary of in-house newcomer study session [Java]
Java knowledge summary
Java Generics Summary
[java] Summary of how to handle char
Java related summary
Summary of changes other than JEP of Java10
[Java] Personal summary of conditional statements (basic)
Java 8 documentation summary
[Java] [Maven3] Summary of how to use Maven3
Regular expressions that match 99% of email addresses
Java Summary of frequently searched type conversions
Java 11 document summary
Easy to trip with Java regular expressions
[Java] Overview of Java
Summary of Java Math.random and import (Calendar)
[java] Summary of how to handle character strings
[Java] Personal summary of classes and methods (basic)
Match IP addresses using regular expressions in Java
Expired collection of java
Predicted Features of Java
Java 12 new feature summary
[Java] Significance of serialVersionUID
[Summary] Java environment preparation
effective java 3rd summary
NIO.2 review of java
Review of java Shilber
Summary of OpenJDK sources
Summary of strong parameters
java --Unification of comments
Understand Java 8 lambda expressions
Summary of jar files
Notes on regular expressions
Java static [Personal summary]
History of Java annotation
Summary of information security
Summary of using FragmentArgs
java (merits of polymorphism)
Thread safe summary ~ Java ~
Java Primitive Specialization Summary