Chinaunix首页 | 论坛 | 博客
  • 博客访问: 299963
  • 博文数量: 101
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 150
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-07 07:42
文章分类

全部博文(101)

文章存档

2016年(12)

2015年(48)

2014年(41)

我的朋友

分类: LINUX

2015-07-23 23:02:04

Regexps are built up from expressions, quantifiers, and assertions. The simplest expression is a character, e.g. x or 5. An expression can also be a set of characters enclosed in square brackets. [ABCD] will match anA or a B or a C or a D. We can write this same expression as [A-D], and an experession to match any captital letter in the English alphabet is written as [A-Z].

正则表达式一般由 表达式,断言,修饰符组成。最简单的表达式为一个字符,比如x或者。一个表达式可以由中括号包括.[ABCD]可以匹配字符A,B,C或者D,也可以写成[A-D]],

一个表达式可以用匹配所有的英文字母[A-Z]

 

A quantifier specifies the number of occurrences of an expression that must be matched. x{1,1} means match one and only one x. x{1,5} means match a sequence of x characters that contains at least one x but no more than five.

 

一个断言可以匹配由任意字符及任意产生次数的表达式。X{1,1} 意味着仅匹配x.x{1,5}
表示匹配15x的字符序列

 

Note that in general regexps cannot be used to check for balanced brackets or tags. For example, a regexp can be written to match an opening html  and its closing , if the  tags are not nested, but if the tags are nested, that same regexp will match an opening  tag with the wrong closing . For the fragment bold bolder, the first  would be matched with the first , which is not correct. However, it is possible to write a regexp that will match nested brackets or tags correctly, but only if the number of nesting levels is fixed and known. If the number of nesting levels is not fixed and known, it is impossible to write a regexp that will not fail.

 

注意,通常正则表达式无法用来检查标识。例如,当标识符没有嵌套的时候,一个正则可以写成匹配起始于与结束于 如果嵌套了,相同的正则表达式会匹配错误的起始标识及终符标识.比如该序列boldbolder,第一个会匹配到第一个,但他不是我们想要的。

不过,我们是可以写出支持嵌套的正则表达式,仅当嵌套次数是给定的情况下,如果嵌套

层次不是可预知的,那么正则就无能为力了

 

Suppose we want a regexp to match integers in the range 0 to 99. At least one digit is required, so we start with the expression [0-9]{1,1}, which matches a single digit exactly once. This regexp matches integers in the range 0 to 9. To match integers up to 99, increase the maximum number of occurrences to 2, so the regexp becomes [0-9]{1,2}. This regexp satisfies the original requirement to match integers from 0 to 99, but it will also match integers that occur in the middle of strings. If we want the matched integer to be the whole string, we must use the anchor assertions, ^ (caret) and $ (dollar).

设想我们想要一个正则去匹配099.最少需要一个数字,所以我们可以用[0-9]{1,1}去配配单个数字.为了能匹配到99,我们需要增大产生数的最大值成为2[0-9]{1,2}.该正则已经能满足原先的需求:匹配099,但是它同样能匹配在字符串中的中间数字。如果我们想完整的匹配整个数字串,我们需要使用断言,^ $

When ^ is the first character in a regexp, it means the regexp must match from the beginning of the string. When $ is the last character of the regexp, it means the regexp must match to the end of the string. The regexp becomes ^[0-9]{1,2}$. Note that assertions, e.g. ^and $, do not match characters but locations in the string.

^为正则的第一个字符时,表示该正则必须以该匹配的字串开始。$则与^相反,表示正则必须匹配该最后的字串。那么该表达式将变成^[0-9]{1,2}$.注意断言^$不会匹配任何字符,只是起到定位字串的作用而已

 

If you have seen regexps described elsewhere, they may have looked different from the ones shown here. This is because some sets of characters and some quantifiers are so common that they have been given special symbols to represent them. [0-9] can be replaced with the symbol \d. The quantifier to match exactly one occurrence, {1,1}, can be replaced with the expression itself, i.e. x{1,1} is the same as x. So our 0 to 99

如果你已经在其他地方看到正则描述,可能有些地方和你见过的存在差异。因为一些字符和断点是较为普遍的,所以给了一个特珠的字符去指定他们。比如[0-9]到可以用\d代替。断言x{1,1}可以直接写成x.

matcher could be written as ^\d{1,2}$. It can also be written ^\d\d{0,1}$, i.e. From the start of the string, match a digit, followed immediately by 0 or 1 digits. In practice, it would be written as ^\d\d?$. The ? is shorthand for the quantifier {0,1}, i.e. 0 or 1 occurrences. ? makes an expression optional. The regexp ^\d\d?$ means From the beginning of the string, match one digit, followed immediately by 0 or 1 more digit, followed immediately by end of string.

所以我们的断言可以写成^\d{1,2}$,同样也可以写成^\d\d{0,1}等。一个字符串以数字开始,后面跟着一个或多个数字.实际我们也可以写成^\d\d?$. ?是断言{0,1}的简单形式,代表0或者1次。

To write a regexp that matches one of the words 'mail' or 'letter' or 'correspondence' but does not match words that contain these words, e.g., 'email', 'mailman', 'mailer', and 'letterbox', start with a regexp that matches 'mail'.

写一个正则匹配 'mail' or 'letter' or 'correspondence' ,但不匹配包含这些字串的表达式,比如, 'email', 'mailman', 'mailer', and 'letterbox',接下来先看一下’mail’ 的正则

 Expressed fully, the regexp is m{1,1}a{1,1}i{1,1}l{1,1}, but because a character expression is automatically quantified by {1,1}, we can simplify the regexp to mail, i.e., an 'm' followed by an 'a' followed by an 'i' followed by an 'l'. Now we can use the vertical bar |, which means or, to include the other two words, so our regexp for matching any of the three words becomes mail|letter|correspondence.

完整的写法,该正则是m{1,1}a{1,1}i{1,1}l{1,1},可是又臭又长,但因为一个字符默认就是{1,1}所以可以写成优雅的形式mail.

Match 'mail' or 'letter'or 'correspondence'. While this regexp will match one of the three words we want to match, it will also match words we don't want to match, e.g., 'email'. To prevent the regexp from matching unwanted words, we must tell it to begin and end the match at word boundaries. First we enclose our regexp in parentheses,(mail|letter|correspondence).

接下来我们用”|”表示逻辑上的或去引用两个单词,可以利用”|”来匹则这三个单词.它只会匹配这三个单词中的一个,比如email是不会被匹配的.为了防止regexp匹配到非法的字符,我们可以一对括号把它们括起来,比如(mail|letter|correspondence).

Parentheses group expressions together, and they identify a part of the regexp that we wish to capture. Enclosing the expression in parentheses allows us to use it as a component in more complex regexps. It also allows us to examine which of the three words was actually matched. To force the match to begin and end on word boundaries, we enclose the regexp in \b word boundary assertions:\b(mail|letter|correspondence)\b. Now the regexp means: Match a word boundary, followed by the regexp in parentheses, followed by a word boundary.

括在圆括号的中的字符,将做为正则独立的一部分去匹配。使用该特性,我们将可以写出更为复杂的正则表达式。同样它也允许我们允许我们审查三个单词中哪个实际上被匹配.对于匹配的始末边界问题,可以引入\b边界断言,(mail|letter|correspondence)\b.现在该正则的意思是匹配一个单词边界。

The \b assertion matches a position in the regexp, not acharacter. A word boundary is any non-word character, e.g., a space, newline, or the beginning or ending of a string.

\b断言在正则匹配位置,不是字符。一个单词边界是任何非可视的字符,比如,一个空格,一个换行符,或者开始处,或者结束处

If we want to replace ampersand characters with the HTML entity &, the regexp to match is simply &. But this regexp will also match ampersands that have already been converted to HTML entities. We want to replace only ampersands that are not already followed by amp;. For this, we need the negative lookahead assertion, (?!__). The regexp can then be written as &(?!amp;), i.e. Match an ampersand that is not followed by amp;.

如果我们想填充一些标识字符,比如使用Html 的标识&, regexp只是简单的匹配 &.但是该正则可以匹配已经被转换了的Html标记,我们仅需要填充标识符使他们后面没有amp;,比如,我们需要匹配一个的前置符(?!_).那么我们的正则可以写成

&(?!amp;),意味着匹配一个标记没有跟随着amp;.

If we want to count all the occurrences of 'Eric' and 'Eirik' in a string, two valid solutions are \b(Eric|Eirik)\band \bEi?ri[ck]\b. The word boundary assertion '\b' is required to avoid matching words that contain either name, e.g. 'Ericsson'. Note that the second regexp matches more spellings than we want: 'Eric', 'Erik', 'Eiric' and 'Eirik'.

如果我们需要对知道'Eric' and 'Eirik'在一个字串出现的次数,有两个方案可以用  \b(Eric|Eirik)\b  \bEi?ri[ck]\b.

这里的\b是必须的,以防止匹配到一部分的内容,比如.” 'Ericsson'

 

Some of the examples discussed above are implemented in the code examples section.

有关于上面的实现可以看看该部分

Characters and Abbreviations for Sets of Characters

Element

Meaning

c

A character represents itself unless it has a special regexp meaning. e.g. c matches the character c.

\c

A character that follows a backslash matches the character itself, except as specified below. e.g., To match a literal caret at the beginning of a string, write \^.

\a

Matches the ASCII bell (BEL, 0x07).

\f

Matches the ASCII form feed (FF, 0x0C).

\n

Matches the ASCII line feed (LF, 0x0A, Unix newline).

\r

Matches the ASCII carriage return (CR, 0x0D).

\t

Matches the ASCII horizontal tab (HT, 0x09).

\v

Matches the ASCII vertical tab (VT, 0x0B).

\xhhhh

Matches the Unicode character corresponding to the hexadecimal number hhhh (between 0x0000 and 0xFFFF).

\0ooo (i.e., \zero ooo)

matches the ASCII/Latin1 character for the octal number ooo (between 0 and 0377).

. (dot)

Matches any character (including newline).

\d

Matches a digit (QChar::isDigit()).

\D

Matches a non-digit.

\s

Matches a whitespace character (QChar::isSpace()).

\S

Matches a non-whitespace character.

\w

Matches a word character (QChar::isLetterOrNumber(), QChar::isMark(), or '_').

\W

Matches a non-word character.

\n

The n-th backreference, e.g. \1, \2, etc.

Note: The C++ compiler transforms backslashes in strings. To include a \ in a regexp, enter it twice, i.e. \\. To match the backslash character itself, enter it four times, i.e. \\\\.

注意匹配\要用\\,如果要匹配\\,就得用\\\\

Sets of Characters

Square brackets mean match any character contained in the square brackets. The character set abbreviations described above can appear in a character set in square brackets. Except for the character set abbreviations and the following two exceptions, characters do not have special meanings in square brackets.

^

The caret negates the character set if it occurs as the first character (i.e. immediately after the opening square bracket). [abc] matches 'a' or 'b' or 'c', but [^abc] matches anything but 'a' or 'b' or 'c'.

-

The dash indicates a range of characters. [W-Z] matches 'W' or 'X' or 'Y' or 'Z'.

Using the predefined character set abbreviations is more portable than using character ranges across platforms and languages. For example, [0-9] matches a digit in Western alphabets but \d matches a digit inany alphabet.

Note: In other regexp documentation, sets of characters are often called "character classes".

Quantifiers

By default, an expression is automatically quantified by {1,1}, i.e. it should occur exactly once. In the following list, E stands for expression. An expression is a character, or an abbreviation for a set of characters, or a set of characters in square brackets, or an expression in parentheses.

E?

Matches zero or one occurrences of E. This quantifier means The previous expression is optional, because it will match whether or not the expression is found. E? is the same as E{0,1}. e.g., dents?matches 'dent' or 'dents'.

E+

Matches one or more occurrences of E. E+ is the same as E{1,}. e.g., 0+ matches '0', '00', '000', etc.

E*

Matches zero or more occurrences of E. It is the same as E{0,}. The * quantifier is often used in error where + should be used. For example, if \s*$ is used in an expression to match strings that end in whitespace, it will match every string because \s*$ means Match zero or more whitespaces followed by end of string. The correct regexp to match strings that have at least one trailing whitespace character is \s+$.

E{n}

Matches exactly n occurrences of E. E{n} is the same as repeating E n times. For example, x{5} is the same as xxxxx. It is also the same as E{n,n}, e.g. x{5,5}.

E{n,}

Matches at least n occurrences of E.

E{,m}

Matches at most m occurrences of E. E{,m} is the same as E{0,m}.

E{n,m}

Matches at least n and at most m occurrences of E.

To apply a quantifier to more than just the preceding character, use parentheses to group characters together in an expression. For example, tag+ matches a 't' followed by an 'a' followed by at least one 'g', whereas(tag)+ matches at least one occurrence of 'tag'.

Note: Quantifiers are normally "greedy". They always match as much text as they can. For example, 0+matches the first zero it finds and all the consecutive zeros after the first zero. Applied to '20005', it matches'20005'. Quantifiers can be made non-greedy, see setMinimal().

阅读(1633) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~