Home > Java > javaTutorial > body text

Introducing the basic syntax and common metacharacters of Java regular expressions

WBOY
Release: 2023-12-26 08:15:57
Original
1333 people have browsed it

Introducing the basic syntax and common metacharacters of Java regular expressions

Introduction to Java regular expression syntax: basic syntax and common metacharacters, specific code examples are required

Overview:
Regular expression is a powerful character String processing tools that can match and process strings through specific syntax rules. In Java, we can use the regular expression class (java.util.regex) to implement pattern matching on strings.

Basic syntax:

  1. Character matching:

    • Ordinary characters: directly match the character itself, such as "abc" matches "abc";
    • Metacharacters: symbols that represent a type of special characters, such as "." represents any character, "[]" represents a character set, "" represents an escape character, etc.
  2. Character limit:

    • "": Matches 0 or more of the previous characters, such as "a" Match "a", "aa", "aaa", etc.;
    • " ": Match one or more of the previous characters, such as "a" matches "a", "aa", "aaa", etc. ;
    • "?": Matches 0 or 1 of the previous characters, such as "a?" matches "a" and "" (empty string);
    • "{n}" : Matches n characters in front of it, such as "a{3}" matches "aaa";
    • "{n,}": Matches at least n characters in front of it, such as "a{2,}" matches " aa", "aaa" etc.
  3. Character set:

    • "[]": Matches any character in the character set, such as "[abc]" matches "a ", "b", "c", etc.;
    • "[^]": matches any character except the characters in the character set, such as "1 "matches any non-"a", "b", "c" characters;
    • "[-]": represents a range in the character set, such as "[a-z]" matches any lowercase letters.
  4. Special characters:

    • "": escape character, used to match some special characters themselves, such as "." matches "." ;
    • ".": Matches any character except newline characters;
    • "^": Matches the beginning of the string, such as "^abc" matches characters starting with "abc" String;
    • "$": matches the end position of the string, such as "xyz$" matches the string ending with "xyz";
    • "|": OR operator, matches multiple One of the patterns, such as "a|b" matches "a" or "b".

Examples of common metacharacters:
The following uses specific code examples to demonstrate the regular expression syntax and the use of common metacharacters in Java.

  1. Matching mobile phone number:

    String regex = "1[3456789]\d{9}";
    String phone = "13912345678";
    boolean isMatch = phone.matches(regex);
    System.out.println(isMatch); // 输出:true
    Copy after login
  2. Matching email address:

    String regex = "\w+@\w+\.\w+";
    String email = "example@example.com";
    boolean isMatch = email.matches(regex);
    System.out.println(isMatch); // 输出:true
    Copy after login
  3. Matching ID number:

    String regex = "\d{17}[0-9Xx]";
    String idCard = "12345678901234567X";
    boolean isMatch = idCard.matches(regex);
    System.out.println(isMatch); // 输出:true
    Copy after login
  4. Extract the domain name in the URL:

    String regex = "https?://(\w+\.)*(\w+\.\w+)";
    String url = "https://www.example.com";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(url);
    if (matcher.find()) {
     String domain = matcher.group(2);
     System.out.println(domain); // 输出:example.com
    }
    Copy after login

Summary:
This article introduces the basic syntax and common elements of Java regular expressions The use of characters is demonstrated with concrete code examples. Regular expressions are powerful and can implement pattern matching and processing of strings, which are very helpful for processing complex string operations. Using regular expressions can quickly and effectively solve some string processing problems and improve development efficiency. In practical applications, regular expressions can be flexibly used for string matching and extraction according to specific needs.


  1. abc

The above is the detailed content of Introducing the basic syntax and common metacharacters of Java regular expressions. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template