📅  最后修改于: 2021-01-05 08:13:02             🧑  作者: Mango
Kotlin正则表达式模式
正则表达式在其函数使用了几种符号(模式)。下面给出一些常用的模式:
Symbol |
Description |
x|y |
Matches either x or y |
xy |
Matches x followed by y |
[xyz] |
Matches either x,y,z |
[x-z] |
Matches any character from x to z |
[^x-z] |
‘^’ as first character negates the pattern. This matches anything outside the range x-z |
^xyz |
Matches expression xyz at beginning of line |
xyz$ |
Matches expression xyz at end of line |
. |
Matches any single character |
正则表达式元符号
Symbol |
Description |
\d |
Matches digits ([0-9]) |
\D |
Matches non-digits |
\w |
Matches word characters |
\W |
Matches non-word characters |
\s |
Matches whitespaces [\t\r\f\n] |
\S |
Matches non-whitespaces |
\b |
Matches word boundary when outside of a bracket. Matches backslash when placed in a bracket |
\B |
Matches non-word boundary |
\A |
Matches beginning of string |
\Z |
Matches end of String |
正则表达式量词模式
Symbol |
Description |
abcd? |
Matches 0 or 1 occurrence of expression abcd |
abcd* |
Matches 0 or more occurrences of expression abcd |
abcd+ |
Matches 1 or more occurrences of expression abcd |
abcd{x} |
Matches exact x occurrences of expression abcd |
abcd{x,} |
Matches x or more occurrences of expression abcd |
abcd{x,y} |
Matches x to y occurrences of expression abcd |
正则表达式样本模式
Pattern |
Description |
([^\s]+(?=\.(jpg|gif|png))\.\2) |
Matches jpg,gif or png images. |
([A-Za-z0-9-]+) |
Matches latter, number and hyphens. |
(^[1-9]{1}$|^[1-4]{1}[0-9]{1}$|^100$) |
Matches any number from 1 to 100 inclusive. |
(#?([A-Fa-f0-9]){3}(([A-Fa-f0-9]){3})?) |
Matches valid hexa decimal color code. |
((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,15}) |
Matches 8 to 15 character string with at least one upper case, one lower case and one digit. |
(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6}) |
Matches email address. |
(\<(/?[^\>]+)\>) |
Matches HTML tags. |