In this section we will briefly describe the basic syntax of regular expressions.
Our goal is not to write the most complex expression, but to write an expression that completes the task.
1. Characters
Character |
Significance |
\ t |
Tabs |
\ n |
Line break |
\ r |
Enter |
\f |
Page change |
\e |
Escape |
\xhh |
Hexadecimal |
\uhhhh |
Unicode hexadecimal |
2. Character classes
Character class |
Significance |
. |
Any character |
[ABC] |
Any character that contains ABC |
[^ABC] |
Any character that does not contain ABC |
[A-za-z] |
A-Z of uppercase and lowercase characters |
[Abc[ijk]] |
Same meaning as a|b|c|i|j|k (and set) |
[A-z&&[ijk]] |
Characters that contain only Ijk (intersection) |
\s |
whitespace (spaces, line breaks, page breaks, tab) |
\s |
Non-whitespace character [^\s] |
\d |
Digital |
\d |
Non-digital |
\w |
Word character [a-za-z0-9] |
\w |
Non-word characters |
3. Boundary characters
Symbol |
Significance |
^ |
Begin |
$ |
End |
\b |
The boundary of the word |
\b |
The boundaries of non-words |
\g |
Last match end |
4. Logic processing
Xy |
x followed by Y. |
X| Y |
X or Y |
X |
Capturing groups |
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 chapter mainly shows the simple syntax of regular expressions and how to create regular expressions in Java.
This chapter is here, thank you.
-----------------------------------
Directory
Understanding java-11.4 Regular Expressions from the beginning (2)-Basic syntax