正则表达式语法规则收集

原创 2004年10月26日 19:58:00
 turnmissile 的 Blog http://blog.csdn.net/turnmissile/

Microsoft已经把正则表达式的规则收录在了msdn里面了,有兴趣的朋友可以自己去研究一下(ms-help://MS.MSDNQTR.2003OCT.1033/cpgenref/html/cpconRegularExpressionsLanguageElements.htm),这里罗列一些我找到的语法元素功能表,大家自己研究吧!

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

转意字符表

Escaped character

Description

ordinary characters

Characters other than . $ ^ { [ ( | ) * + ? / match themselves.

/a

Matches a bell (alarm) /u0007.

/b

Matches a backspace /u0008 if in a [] character class; otherwise, see the note following this table.

/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

Match+es an ASCII control character; for example, /cC is control-C.

/u0020

Matches a Unicode character using 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.

Note   The escaped character /b is a special case. In a regular expression, /b denotes a word boundary (between /w and /W characters) except within a [] character class, where /b refers to the backspace character. In a replacement pattern, /b always denotes a backspace.

 

字符集

A character class is a set of characters that will find a match if any one of the characters included in the set matches. The following table summarizes character matching syntax.

Character 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.

You can find the Unicode category a character belongs to with the method

 

正则表达式选项

and ECMAScript are not allowed inline.

RegexOption member

Inline character

Description

None

N/A

Specifies that no options are set.

IgnoreCase

i

Specifies case-insensitive matching.

Multiline

m

Specifies multiline mode. Changes the meaning of ^ and $ so that they match at the beginning and end, respectively, of any line, not just the beginning and end of the whole string.

ExplicitCapture

n

Specifies that the only valid captures are explicitly named or numbered groups of the form (?<name>…). This allows parentheses to act as noncapturing groups without the syntactic clumsiness of (?:…).

Compiled

N/A

Specifies that the regular expression will be compiled to an assembly. Generates Microsoft intermediate language (MSIL) code for the regular expression; yields faster execution at the expense of startup time.

Singleline

s

Specifies single-line mode. Changes the meaning of the period character (.) so that it matches every character (instead of every character except /n).

IgnorePatternWhitespace

x

Specifies that unescaped white space is excluded from the pattern and enables comments following a number sign (#). (For a list of escaped white-space characters, see Character Escapes.) Note that white space is never eliminated from within a character class.

RightToLeft

N/A

Specifies that the search moves from right to left instead of from left to right. A regular expression with this option moves to the left of the starting position instead of to the right. (Therefore, the starting position should be specified as the end of the string instead of the beginning.) This option cannot be specified in midstream, to prevent the possibility of crafting regular expressions with infinite loops. However, the (?<) lookbehind constructs provide something similar that can be used as a subexpression.

RightToLeft changes the search direction only. It does not reverse the substring that is searched for. The lookahead and lookbehind assertions do not change: lookahead looks to the right; lookbehind looks to the left.

ECMAScript

N/A

Specifies that ECMAScript-compliant behavior is enabled for the expression. This option can be used only in conjunction with the IgnoreCase and Multiline flags. Use of ECMAScript with any other flags results in an exception.

CultureInvariant

N/A

Specifies that cultural differences in language is ignored. See Performing Culture-Insensitive Operations in the RegularExpressions Namespace for more information.

 

 

Atomic Zero-Width Assertions

Assertion

Description

^

Specifies that the match must occur at the beginning of the string or the beginning of the line. For more information, see the Multiline option in Regular Expression Options.

$

Specifies that the match must occur at the end of the string, before /n at the end of the string, or at the end of the line. For more information, see the Multiline option in Regular Expression Options.

/A

Specifies that the match must occur at the beginning of the string (ignores the Multiline option).

/Z

Specifies that the match must occur at the end of the string or before /n at the end of the string (ignores the Multiline option).

/z

Specifies that the match must occur at the end of the string (ignores the Multiline option).

/G

Specifies that the match must occur at the point where the previous match ended. When used with Match.NextMatch(), this ensures that matches are all contiguous.

/b

Specifies that the match must occur on a boundary between /w (alphanumeric) and /W (nonalphanumeric) characters. The match must occur on word boundaries — that is, at the first or last characters in words separated by any nonalphanumeric characters.

/B

Specifies that the match must not occur on a /b boundary.

 

 

 

数量

Quantifier

Description

*

Specifies zero or more matches; for example, /w* or (abc)*. Equivalent to {0,}.

+

Specifies one or more matches; for example, /w+ or (abc)+. Equivalent to {1,}.

?

Specifies zero or one matches; for example, /w? or (abc)?. Equivalent to {0,1}.

{n}

Specifies exactly n matches; for example, (pizza){2}.

{n,}

Specifies at least n matches; for example, (abc){2,}.

{n,m}

Specifies at least n, but no more than m, matches.

*?

Specifies the first match that consumes as few repeats as possible (equivalent to lazy *).

+?

Specifies as few repeats as possible, but at least one (equivalent to lazy +).

??

Specifies zero repeats if possible, or one (lazy ?).

{n}?

Equivalent to {n} (lazy {n}).

{n,}?

Specifies as few repeats as possible, but at least n (lazy {n,}).

{n,m}?

Specifies as few repeats as possible between n and m (lazy {n,m}).

 

 

组构造

Grouping constructs allow you to capture groups of subexpressions and to increase the efficiency of regular expressions with noncapturing lookahead and lookbehind modifiers. The following table describes the Regular Expression Grouping Constructs.

Grouping construct

Description

(   )

Captures the matched substring (or noncapturing group; for more information, see the ExplicitCapture option in Regular Expression Options). Captures using () are numbered automatically based on the order of the opening parenthesis, starting from one. The first capture, capture element number zero, is the text matched by the whole regular expression pattern.

(?<name>   )

Captures the matched substring into a group name or number name. The string used for name must not contain any punctuation and it cannot begin with a number. You can use single quotes instead of angle brackets; for example, (?'name').

(?<name1-name2> )

Balancing group definition. Deletes the definition of the previously defined group name2 and stores in group name1 the interval between the previously defined name2 group and the current group. If no group name2 is defined, the match backtracks. Because deleting the last definition of name2 reveals the previous definition of name2, this construct allows the stack of captures for group name2 to be used as a counter for keeping track of nested constructs such as parentheses. In this construct, name1 is optional. You can use single quotes instead of angle brackets; for example, (?'name1-name2').

(?:   )

Noncapturing group.

(?imnsx-imnsx:   )

Applies or disables the specified options within the subexpression. For example, (?i-s: ) turns on case insensitivity and disables single-line mode. For more information, see Regular Expression Options.

(?=   )

Zero-width positive lookahead assertion. Continues match only if the subexpression matches at this position on the right. For example, /w+(?=/d) matches a word followed by a digit, without matching the digit. This construct does not backtrack.

(?!   )

Zero-width negative lookahead assertion. Continues match only if the subexpression does not match at this position on the right. For example, /b(?!un)/w+/b matches words that do not begin with un.

(?<=   )

Zero-width positive lookbehind assertion. Continues match only if the subexpression matches at this position on the left. For example, (?<=19)99 matches instances of 99 that follow 19. This construct does not backtrack.

(?<!   )

Zero-width negative lookbehind assertion. Continues match only if the subexpression does not match at the position on the left.

(?>   )

Nonbacktracking subexpression (also known as a "greedy" subexpression). The subexpression is fully matched once, and then does not participate piecemeal in backtracking. (That is, the subexpression matches only strings that would be matched by the subexpression alone.)

Named captures are numbered sequentially, based on the left-to-right order of the opening parenthesis (like unnamed captures), but numbering of named captures starts after all unnamed captures have been counted. For instance, the pattern ((?<One>abc)/d+)?(?<Two>xyz)(.*) produces the following capturing groups by number and name. (The first capture (number 0) always refers to the entire pattern).

Number

Name

Pattern

0

0 (default name)

((?<One>abc)/d+)?(?<Two>xyz)(.*)

1

1 (default name)

((?<One>abc)/d+)

2

2 (default name)

(.*)

3

One

(?<One>abc)

4

Two

(?<Two>xyz)

 

 

Backreference Constructs

The following table lists optional parameters that add backreference modifiers to a regular expression.

Backreference construct

Definition

/number

Backreference. For example, (/w)/1 finds doubled word characters.

/k<name>

Named backreference. For example, (?<char>/w)/k<char> finds doubled word characters. The expression (?<43>/w)/43 does the same. You can use single quotes instead of angle brackets; for example, /k'char'.

Note the ambiguity between octal escape codes and /number backreferences that use the same notation. See Backreferences for details on how the regular expression engine resolves the ambiguity.

 

其他

The following table lists subexpressions that modify a regular expression.

Construct

Definition

(?imnsx-imnsx)

Sets or disables options such as case insensitivity to be turned on or off in the middle of a pattern. For information on specific options, see Regular Expression Options. Option changes are effective until the end of the enclosing group. See also the information on the grouping construct (?imnsx-imnsx: ), which is a cleaner form.

(?# )

Inline comment inserted within a regular expression. The comment terminates at the first closing parenthesis character.

# [to end of line]

X-mode comment. The comment begins at an unescaped # and continues to the end of the line. (Note that the x option or the RegexOptions.IgnorePatternWhitespace enumerated option must be activated for this kind of comment to be recognized.)

版权声明:本文为博主原创文章,未经博主允许不得转载。

正则表达式的基本语法规则

一、正则表达式的概念     正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。 ...
  • napoleonwxu
  • napoleonwxu
  • 2015年09月11日 14:54
  • 707

正则表达式语法规则

正则表达式就是描述了一类字符串的特征,通过这个特征与特定的函数配合使用,对其它的字符串进行匹配、查找、替换、分割操作! 正则表达式语法规则 正则表达式描述了一类字符串的特征,通过这个特征...
  • confusing_awakening
  • confusing_awakening
  • 2017年12月19日 11:52
  • 43

正则表达式的常用语法规则

[]:表示[]内的任意字符,如:[013] 表示匹配0,1或3任意一个字符    [0-9][0-9]:表示任意一个两位数00-99    [a-zA-Z]:表示匹配任意的一个字母...
  • quan648997767
  • quan648997767
  • 2013年05月14日 21:48
  • 475

正则表达式的语法规则

行定位符(^和$)   行定位符就是用来描述字串的边界。“^”表示行的开始;“$”表示行的结尾。如:    ^tm   该表达式表示要匹配字串tm的开始位置是行头,如tm equal Tomo...
  • linshichen
  • linshichen
  • 2016年03月04日 15:19
  • 131

js 正则表达式语法规则

1. 正则表达式规则 1.1 普通字符     字母、数字、汉字、下划线、以及后边章节中没有特殊定义的标点符号,都是"普通字符"。表达式中的普通字符,在匹配一个字符串的时候,匹配与之相同的一个...
  • xiaosong000000
  • xiaosong000000
  • 2013年12月31日 15:18
  • 243

正则表达式基本语法规则

1.正则表达式基本语法 两个特殊的符号'^'和'$'。他们的作用是分别指出一个字符串的开始和结束。例子如下: "^The":表示所有以"The"开始的字符串("There","The cat"...
  • u010056403
  • u010056403
  • 2014年05月23日 22:40
  • 356

C#正则表达式语法规则详解

正则表达式通常包含字母文本(Literaltext)和元字符(metacharacter)  字母文本指的是普通文本如"abcde"可匹配字符串中任何包含"abcde"的字符串。 元字符则更加...
  • xiaojia7283
  • xiaojia7283
  • 2014年02月13日 16:34
  • 515

简述正则表达式的语法规则(一)

一、前序 正则表达式具有一套非常完整的、可以编写模式的语法体系。 正则表达式通过构建具有特定规则的模式,与输入的字符串信息进行比较,在特定的函数中使用,从而实现字符串的匹配、查找、替换及分隔等操作。 ...
  • joyksk
  • joyksk
  • 2017年06月22日 18:54
  • 249

Python3正则表达式(一)基本语法规则

正则表达式是一个很强大的字符串处理工具,它能帮助我们方便的检查一个字符串是否与某种模式匹配。一、python中正则表达式的基本语法规则 序号 语法 解释 表达式 成功匹配对象 1...
  • m0_37852369
  • m0_37852369
  • 2017年12月18日 23:43
  • 108

正则表达式的基本语法和在Python下的使用

正则表达式基本语法常用正则表达式符号 符号 说明 举例 literal 匹配字符串的值 Foo re1|re2 匹配正则表达式re1或re2 foo . 匹配...
  • m_buddy
  • m_buddy
  • 2017年05月10日 10:14
  • 340
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:正则表达式语法规则收集
举报原因:
原因补充:

(最多只允许输入30个字)