Like you who love programming!
Learn SpringBoot actual combat course https://edu.csdn.net/course/detail/31433
Learn SpringCloud introductory course https://edu.csdn.net/course/detail/31451
Preface
Regular expression is a powerful string manipulation tool, which can achieve complex format string matching, replacement, segmentation, search, etc.
Regular expressions are supported in various languages, such as: java, Javascript, python, etc. The syntax is universal.
Regular expressions are composed of various symbols, and the key point is to learn the use of these symbols.
Character match
Use [] to match a single character, () to match multiple words
symbol | usage |
---|---|
[abc] | Find any character in square brackets |
[^abc] | Find any character not in square brackets |
[0-9] | Find any number from 0-9 |
[a-z] | Find any character from lowercase a to z |
(red|blue|green) | Find any item in parentheses |
Beginning and end
- ^ Matches the beginning of a string, such as (^a) is to match a string beginning with the letter a
- $ Matches the end of a string, for example (b$) matches a string ending with the letter b
Adding ^ and $ at the beginning and end of the regular expression can effectively match the complete string:
/^表达式$/
Metacharacter
symbol | usage |
---|---|
. | Find any single character, except for newlines |
\w | Any letter or number or underscore, any one of [A-Za-z0-9_] |
\W | Find non-word characters, equivalent to [^A_Za_z0_9_] |
\d | Match a digit character, equivalent to [0-9] |
\D | Match a non-digit character, equivalent to [^0-9] |
\s | Matches any blank characters, including spaces, tabs, newlines, etc. Equivalent to [\f\n\r\t\v] |
\S | Match any non-white space character, equivalent to [^\f\n\r\t\v] |
\b | Match the word boundary, the position between the word and the space, such as'er\b' can match the "er" in "never", but not the "er" in "verb" |
\B | Match non-word boundaries,'er\B' can match the'er' in'verb' but not the'er' in'never' |
\0 | Find NUL characters. |
\n | Match a newline character |
\f | Match a form feed |
\r | Matches a carriage return |
\t | Matches a tab |
\ v | Matches a vertical tab |
\xxx | Find a character specified by the octal number xxx |
\ xdd | Find the character specified by the hexadecimal number dd |
\ uxxxx | Find the Unicode character specified by xxxx in hexadecimal number. |
quantifier
symbol | usage |
---|---|
n+ | Matches any string containing at least one n |
n* | Match zero or more strings of n |
n? | Match zero or one string of n |
n{x} | Match the sequence string containing x n |
n {x, y} | Match at least x and at most y strings of n |
n{x,} | Matches at least x strings |
n$ | Match strings ending in n |
^n | Match strings starting with n |
?=n | Match the specified n string immediately after it |
?!n | Matches the n string that does not follow the specified |
Commonly used regular expressions
Some common regular expressions
phone number
^1[3|4|5|7|8]\d{9}$
Decimal
^(-?\d+)(\.\d+)?$
email address
[a-zA-Z_]{1,}[0-9]{0,}@(([a-zA-z0-9]-*){1,}\\.){1,3}[a-zA-z\\-]{1,}
HTML tags
/<(.*)>.*<\/\1>|<(.*) \/>/
Chinese characters
[\u4e00-\u9fa5]
QQ number
(\d+)\.(\d+)\.(\d+)\.(\d+)
IP
(\d+)\.(\d+)\.(\d+)\.(\d+)
Use regular expressions
Java implements regular expression matching API:
- Pattern expression pattern
- Matcher
Pattern class
Creation method
Pattern.compile(regEx)
忽略大小写
Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
Matcher class
Creation method
pattern对象.matcher(字符串)
Common method
- boolean matches() whether to match the complete string
Email verification case
public static void main(String[] args) {
// 要验证的字符串
String str = "[email protected]";
// 邮箱验证规则
String regEx = "[a-zA-Z_]{1,}[0-9]{0,}@(([a-zA-z0-9]-*){1,}\\.){1,3}[a-zA-z\\-]{1,}";
// 编译正则表达式
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(str);
// 字符串是否与正则表达式相匹配
boolean rs = matcher.matches();
System.out.println(rs);
}
Common method
- Whether boolean find() can search the defined content in the string
- String group(String name) returns the searched string content
Search the content of src in multiple img tags
String str = "<img src=\"http://www.xx.com/1.jpg\"><img src=\"http://www.xx.com/2.jpg\">";
Pattern pattern = Pattern.compile("<img src=\"(?<url>.+?)\">");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group("url"));
}
String
Some methods in the String class also use regular expressions
such as:
- String replaceAll(String regex,String replacement)
- boolean matches(String regex)
- String[] split(String regex)
Replace numbers with *
String str = "123456Java";
String str2 = str.replaceAll("\\d", "*");
Match phone number
String tel = "1567667443";
boolean matches = tel.matches("^1[3|4|5|7|8]\\d{9}$");
Split character
String words = "中国.湖北.武汉";
String[] strings = words.split("\\.");
End
If you need to learn other Java knowledge, poke here ultra-detailed knowledge of Java Summary