As we all know, regular expressions are a specification that can be used for pattern matching and substitution. A regular expression is a literal pattern consisting of ordinary characters (such as characters A through Z) and special characters (metacharacters) that describe one or more strings to be matched when finding the body of the text. A regular expression is used as a template to match a character pattern with the string being searched for.
Since jdk1.4 launched the Java.util.regex package, it provides us with a good Java regular Expression application platform.
Because the regular expression is a very complex system, so I just take a few examples of the concept of getting started, more see the relevant books and explore on their own.
\\ 反斜杠
\t 间隔 ('\u0009')
\n 换行 ('\u000A')
\r 回车 ('\u000D')
\d 数字 等价于[0-9]
\D 非数字 等价于[^0-9]
\s 空白符号 [\t\n\x0B\f\r]
\S 非空白符号 [^\t\n\x0B\f\r]
\w 单独字符 [a-zA-Z_0-9]
\W 非单独字符 [^a-zA-Z_0-9]
\f 换页符
\e Escape
\b 一个单词的边界
\B 一个非单词的边界
\G 前一个匹配的结束
^为限制开头
^java 条件限制为以Java为开头字符
$为限制结尾
java$ 条件限制为以java为结尾字符
. 条件限制除\n以外任意一个单独字符
java.. 条件限制为java后除换行外任意两个字符
To add a specific restriction condition []
[a-z] 条件限制在小写a to z范围中一个字符
[A-Z] 条件限制在大写A to Z范围中一个字符
[a-zA-Z] 条件限制在小写a to z或大写A to Z范围中一个字符
[0-9] 条件限制在小写0 to 9范围中一个字符
[0-9a-z] 条件限制在小写0 to 9或a to z范围中一个字符
[0-9[a-z]] 条件限制在小写0 to 9或a to z范围中一个字符(交集)
[] Add in ^ post-add again restrictive conditions [^]
[^a-z] 条件限制在非小写a to z范围中一个字符
[^A-Z] 条件限制在非大写A to Z范围中一个字符
[^a-zA-Z] 条件限制在非小写a to z或大写A to Z范围中一个字符
[^0-9] 条件限制在非小写0 to 9范围中一个字符
[^0-9a-z] 条件限制在非小写0 to 9或a to z范围中一个字符
[^0-9[a-z]] 条件限制在非小写0 to 9或a to z范围中一个字符(交集)
When a limit condition is more than 0 occurrences of a particular character, you can use the *
J* 0个以上J
.* 0个以上任意字符
J.*D J与D之间0个以上任意字符
When a limit condition is more than 1 occurrences of a particular character, you can use the +
J+ 1个以上J
.+ 1个以上任意字符
J.+D J与D之间1个以上任意字符
Can you use when a limit condition is 0 or 1 times the occurrence of a particular character?
JA? J或者JA出现
Limit to consecutive occurrences of the specified secondary number character {a}
J{2} JJ
J{3} JJJ
Text a more than, and {A,}
J{3,} JJJ,JJJJ,JJJJJ,???(3次以上J并存)
Text above, B below {a,b}
J{3,5} JJJ或JJJJ或JJJJJ
Both Take one |
J|A J或A
Java|Hello Java或Hello
A combination type is specified in "()"
For example, I query <a href=\ "index.html\" >index</a> <a href></a> between the data, can write <a.*href=\ ". *\" > (. +?) </a>
When you use the Pattern.compile function, you can add parameters that control the matching behavior of the regular expression:
Pattern Pattern.compile(String regex, int flag)