Java-11.4 Regular Expression (2)-basic syntax
This section briefly introduces the basic syntax of regular expressions.
Our goal is not to write the most complex expressions, but to write the expressions that can complete tasks.
1. Character
| Character |
Meaning |
| \ T |
Tab |
| \ N |
Line feed |
| \ R |
Enter |
| \ F |
Form feed |
| \ E |
Escape |
| \ Xhh |
Hexadecimal |
| \ Uhhhh |
Unicode hexadecimal |
2. character classes
| Character class |
Meaning |
| . |
Any character |
| [Abc] |
Any character containing abc |
| [^ Abc] |
Does not contain any characters of abc |
| [A-zA-z] |
Uppercase and lowercase letters of a-z |
| [Abc [ijk] |
Same as a | B | c | I | j | k (union) |
| [A-z & [ijk] |
Only ijk characters (intersection) |
| \ S |
Blank space (space, line feed, page feed, tab) |
| \ S |
Non-blank character [^ \ s] |
| \ D |
Number |
| \ D |
Non-numeric |
| \ W |
Word character [a-zA-z0-9] |
| \ W |
Non-word characters |
3. boundary operator
| Symbol |
Meaning |
| ^ |
Start |
| $ |
End |
| \ B |
Word boundary |
| \ B |
Non-word boundary |
| \ G |
Last match ended |
4. logic processing
| XY |
X follows y. |
| X | Y |
X or y |
| (X) |
Capture Group |
5. Create a regular expression
package com.ray.ch11;public class Test {public static void main(String[] args) {String a = "ddd012";for (String pattern : new String[] { "^[a-z]+[0-9]+$", "\\d+", "-?\\a+" }) {System.out.println(a.matches(pattern));}}}
Output:
True
False
False
Summary: This section describes the simple Syntax of regular expressions and how to create regular expressions in java.