Home >Java >javaTutorial >What are the java regular expression syntax

What are the java regular expression syntax

小老鼠
小老鼠Original
2023-12-25 15:35:02917browse

The basic syntax of Java regular expressions includes metacharacters, predefined patterns, character classes, quantifier modifiers, escaping, grouping, non-capturing grouping, look-ahead assertions, named capture groups, backward references, etc. Detailed introduction: 1. Metacharacters: ".": matches any single character (except newline); "^": matches the beginning of the input string; "$": matches the end of the input string; "*": Matches the previous subexpression zero or more times; " ": matches the previous subexpression one or more times; "?": matches the previous subexpression zero or one time, etc.

What are the java regular expression syntax

Operating system for this tutorial: Windows 10 system, Dell G3 computer.

Java Regular Expression (Regular Expression) is a powerful text processing tool that uses specific patterns to match, find, or manipulate text. Here is some basic syntax for Java regular expressions:

  1. Metacharacters:

    • .: Matches any single character (except newline).
    • ^: Matches the beginning of the input string.
    • $: Matches the end position of the input string.
    • *: Matches the previous subexpression zero or more times.
    • : Matches the previous subexpression one or more times.
    • ?: Matches the previous subexpression zero or one time.
    • {n}: n is a non-negative integer. Match determined n times.
    • {n,}: n is a non-negative integer. Match at least n times.
    • {n,m}: m and n are both non-negative integers, where n
    • \d: Matches a numeric character. Equivalent to [0-9].
    • \D: Matches a non-numeric character. Equivalent to [^0-9].
    • \s: Matches any whitespace characters, including spaces, tabs, form feeds, etc. Equivalent to [\f\n\r\t\v].
    • \S: Matches any non-whitespace character. Equivalent to [^\f\n\r\t\v].
    • \w: Matches any letter, number, or underline character including an underscore. Equivalent to '[A-Za-z0-9_]'.
    • \W: Matches any non-word character. Equivalent to '[^A-Za-z0-9_]'.
  2. Predefined patterns:

    • ^$: Empty line (consisting only of whitespace characters).
    • .: Any single character (except newline).
    • \n: newline character.
    • \r: carriage return character.
    • \t: Tab character.
  3. Character class:

    • []: Character set, such as [abc] will match any character in a, b or c.
    • [^]: Negative character set, such as [^abc] will match any character except a, b, c.
  4. Modifiers of quantifiers:

    • ?: The previous character or subexpression appears 0 or 1 times.
    • *: The previous character or subexpression appears 0 or more times.
    • : The previous character or subexpression appears 1 or more times.
    • {n}: n is a non-negative integer, and the previous character or subexpression appears exactly n times.
    • {n,}: n is a non-negative integer, and the previous character or subexpression appears at least n times.
    • {n,m}: m and n are both non-negative integers, where n
  5. Escape: Use backslash (\) to escape special characters, such as \. to match actual dot characters instead of special characters.

  6. Grouping: Use parentheses for grouping, for example (ab)* means matching "ab" zero or more times.

  7. Non-capturing grouping: Use parentheses and add a question mark in front to implement non-capturing grouping, for example (?:ab)* means matching "ab" zero or more times, But the group contents are not captured.

  8. Look-ahead assertion: Use (?=...) and (?!...) to make a look-ahead assertion, for example (?=abc) represents a string that must follow immediately It is "abc", and (?!abc) means that the string immediately following cannot be "abc".

  9. Named capturing group: Use (?...) to define a named capturing group, for example (?202[0-9]{2}) Represents matching years between 2020 and 2029 and capturing them as a group named "year".

  10. Back reference: Use \n (where n is a positive integer) for back reference, referencing the matching content of the nth capture group, such as continuous occurrences in the pattern string The same character twice can be represented by a back reference. For example, the same character that appears twice in a row in a pattern string can be represented by a back reference. For example, the same character that appears twice in a pattern string can be represented by a back reference. Quote

The above is the detailed content of What are the java regular expression syntax. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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