欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

正则表达式速查表(ASP.NET)

程序员文章站 2024-03-07 10:11:44
出处:regexlib.com regular expression cheat sheet (.net) 元字符 说明 ^ 匹配字符串...

出处:regexlib.com regular expression cheat sheet (.net)

元字符 说明
^ 匹配字符串的开始位置
$ 匹配字符串的结束位置
. 匹配任意单个字符(换行符 \n 除外)
| 交替
{…} 指定要限定的数量
[...] 指定要匹配的字符集
(…) 对表达式进行逻辑分组
* 匹配零或多个前面的表达式
+ 匹配一或多个前面的表达式
? 匹配零或一个前面的表达式
\ 放在上面任何一个字符之前,表示匹配该字符本身。放在其他特殊字符后面,表示字符转义(见下面)
字符转义 说明
原始字符 除 . $ ^ { [ ( | ) ] } * + ? \ 之外的字符均匹配自身
\a 匹配铃声(闹铃)\u0007
\b 在[]中匹配一个空格 \u0008,在其他情况下匹配字边界(位于 \w 和 \w 字符之间)
\t 匹配制表符 \u0009
\r 匹制回车符 \u000d
\v 匹配垂直制表符 \u000b
\f 匹配换页符 \u000c
\n 匹配换行符 \u000a
\e 匹配退出键(符) \u001b
\040 匹配以八进制表示的 ascii 字符(最多三位数);在没有前导零的情况下,如果只有一位数字或者相应数字与某个捕获组的编号对应,那就是反向引用(backreference)。字符 \040 表示一个空格。
\x20 匹配以十六进制表示的 ascii 字符(两位数)
\cc 匹配 ascii 控制符,例如 \cc 匹配 ctrl+c
\u0020 匹配以十六进制表示的 unicode 字符
\* 反斜杠后面如果不是一个可转义的字符,则匹配该字符本身。例如,\* 就相当于\x2a
字符类 说明
. 匹配除 \n 之外的任意字符。
[aeiou] 匹配特定字符集中包含的任意一个字符
[^aeiou] 匹配特定字符集中不包含的任意一个字符
[0-9a-fa-f] 连字符(-)用来指定连续的字符范围
\p{name} 匹配由{name}指定的命名字符类中的任意字符
\p{name} 匹配不包含在{name}指定的组或块范围中的文本
\w 匹配英文数字字母字符,在指定兼容ecmascript的情况下,等价于[a-za-z0-9]
\w 匹配非英文数字字母字符,在指定兼容ecmascript的情况下,等价于[^a-za-z0-9]
\s 匹配任意空白字符,在指定兼容ecmascript的情况下,等价于[\f\n\r\t\v]
\s 匹配任意非空白字符,在指定兼容ecmascript的情况下,等价于[^\f\n\r\t\v]
\d 匹配数字字符,在指定兼容ecmascript的情况下,等价于[0-9]
\d 匹配非数字字符,在指定兼容ecmascript的情况下,等价于[^0-9]

英文版:

metacharacters defined

mchar definition
^ start of a string.
$ end of a string.
. any character (except \n newline)
| alternation.
{...} explicit quantifier notation.
[...] explicit set of characters to match.
(...) logical grouping of part of an expression.
* 0 or more of previous expression.
+ 1 or more of previous expression.
? 0 or 1 of previous expression; also forces minimal matching when an expression might match several strings within a search string.
\ preceding one of the above, it makes it a literal instead of a special character. preceding a special matching character, see below.

metacharacter examples

pattern sample matches
^abc abc, abcdefg, abc123, ...
abc$ abc, endsinabc, 123abc, ...
a.c abc, aac, acc, adc, aec, ...
bill|ted ted, bill
ab{2}c abbc
a[bb]c abc, abc
(abc){2} abcabc
ab*c ac, abc, abbc, abbbc, ...
ab+c abc, abbc, abbbc, ...
ab?c ac, abc
a\sc a c

character escapes http://tinyurl.com/5wm3wl

escaped char description
ordinary characters characters other than . $ ^ { [ ( | ) ] } * + ? \ match themselves.
\a matches a bell (alarm) \u0007.
\b matches a backspace \u0008 if in a []; otherwise matches a word boundary (between \w and \w characters).
\t matches a tab \u0009.
\r matches a carriage return \u000d.
\v matches a vertical tab \u000b.
\f matches a form feed \u000c.
\n matches a new line \u000a.
\e matches an escape \u001b.
\040 matches an ascii character as octal (up to three digits); numbers with no leading zero are backreferences if they have only one digit or if they correspond to a capturing group number. (for more information, see backreferences.) for example, the character \040 represents a space.
\x20 matches an ascii character using hexadecimal representation (exactly two digits).
\cc matches an ascii control character; for example \cc is control-c.
\u0020 matches a unicode character using a hexadecimal representation (exactly four digits).
\* when followed by a character that is not recognized as an escaped character, matches that character. for example, \* is the same as \x2a.

character classes http://tinyurl.com/5ck4ll

char class description
. matches any character except \n. if modified by the singleline option, a period character matches any character. for more information, see regular expression options.
[aeiou] matches any single character included in the specified set of characters.
[^aeiou] matches any single character not in the specified set of characters.
[0-9a-fa-f] use of a hyphen (–) allows specification of contiguous character ranges.
\p{name} matches any character in the named character class specified by {name}. supported names are unicode groups and block ranges. for example, ll, nd, z, isgreek, isboxdrawing.
\p{name} matches text not included in groups and block ranges specified in {name}.
\w matches any word character. equivalent to the unicode character categories [\p{ll}\p{lu}\p{lt}\p{lo}\p{nd}\p{pc}]. if ecmascript-compliant behavior is specified with the ecmascript option, \w is equivalent to [a-za-z_0-9].
\w matches any nonword character. equivalent to the unicode categories [^\p{ll}\p{lu}\p{lt}\p{lo}\p{nd}\p{pc}]. if ecmascript-compliant behavior is specified with the ecmascript option, \w is equivalent to [^a-za-z_0-9].
\s matches any white-space character. equivalent to the unicode character categories [\f\n\r\t\v\x85\p{z}]. if ecmascript-compliant behavior is specified with the ecmascript option, \s is equivalent to [ \f\n\r\t\v].
\s matches any non-white-space character. equivalent to the unicode character categories [^\f\n\r\t\v\x85\p{z}]. if ecmascript-compliant behavior is specified with the ecmascript option, \s is equivalent to [^ \f\n\r\t\v].
\d matches any decimal digit. equivalent to \p{nd} for unicode and [0-9] for non-unicode, ecmascript behavior.
\d matches any nondigit. equivalent to \p{nd} for unicode and [^0-9] for non-unicode, ecmascript behavior.