3.2.1 Regular Expression syntax (3), 3.2.1 Regular Expression

Source: Internet
Author: User

3.2.1 Regular Expression syntax (3), 3.2.1 Regular Expression

(? (Id/name) yes-pattern | no-pattern)

First, determine whether the id or name exists. If yes, use the yes-pattern Rule to match. If no-pattern exists, use the no-pattern Rule. And no-pattern is optional and does not exist.

Example:

Print ('(<)? (\ W + @ \ w + (? : \. \ W + )(? (1)> | $ )')

M = re. findall (U' (<)? (\ W + @ \ w + (? : \. \ W + )(? (1)> | $ )',

U' <a@t1.cn <a@t2.cn> a@t3.cn> a@t4.cn ')

If m:

Print (m)

The output is as follows:

(<)? (\ W + @ \ w + (? : \. \ W + )(? (1)> | $)

[('<', 'A @ t2.cn '), ('', 'a @ t4.cn')]

The following describes escape characters, which are mainly composed of '\' and specific characters. If the specified character is not in the following list, re-judgment skips '\' and uses the second character for determination. For example, if \ $ cannot be found in the following characters, it will be used as $.

\ Number

Match the group with the same number. The group number starts from 1. For example (. +) \ 1 will match the 'or '5555', but it will not match the 'theth' mode, because (. +) there is a space between \ 1 and \ 1. \ 1 indicates and (. +) same matching group. This method can only match numbers 1 to 99. If the number is greater than 99, it is regarded as the octal number, and the number starting with zero is also considered as the octal number. Note that any number in square brackets [], including escape characters, is considered as common characters.

Example:

Print (R' (. +) \ 1 ')

M = re. findall (R' (. +) \ 1', r'abc abc abcabc 55 56 57 57 ')

If m:

Print (m)

The output result is as follows:

(. +) \ 1

['Abc', '5', '57 ']

In this example, note that regular expression rule characters must use the original string starting with r, or use dual \ To represent numbers. Otherwise, \ 1 indicates other characters.

\

It indicates matching only from the starting position of the string.

Example:

Print ('\ aabc ')

M = re. findall (U' \ aabc', u'abc aabc ')

If m:

Print (m)

M = re. findall (u 'abc', u 'abc aabc ')

If m:

Print (m)

The output result is as follows:

\ Aabc

['Abc']

['Abc', 'abc']

Here we can see that after using \ A, only one is matched, and without this flag, it will match the abc in any place.

\ B

Matches an empty string before or after a word. The character range defined by a word is unicode ascii or underline. An empty string is defined as a space, non-ASCII, or non-underline character. For example, R' \ bcai \ B 'indicates 'cai', 'cai. ', and' (cai) ', but does not match 'caimi' or 'cai8 '.

Example:

Print (R' \ B ')

M = re. findall (R' \ bcai \ B ', r'caimouse cai cai8 ')

If m:

Print (m)

The output is as follows:

\ B

['Cai ']

\ B

Match a non-empty string at the beginning or end of a word. For example, r'cai \ B 'indicates matching 'caimi', 'cai8', and 'cai _', but does not match 'cai ', 'cai.', 'cai! '. In this sense, it is exactly the opposite of \ B.

Example:

Print (R' \ B ')

M = re. findall (r 'cai \ B ', r 'caimouse cai cai8 ')

If m:

Print (m)

The output is as follows:

\ B

['Cai ', 'cai']

\ D

In UNICODE mode:

Match any UNICODE number (which is equivalent to [Nd] in UNICODE). Including [0-9] and other numeric characters. If you set the ASCII flag, it is limited to [0-9]. If so, it is better to directly use [0-9.

In the 8-Bit mode:

Match any number character, equivalent to [0-9].

Example:

Print (R' \ D ')

M = re. findall (r 'cai \ d', r 'caimouse cai cai8 ')

If m:

Print (m)

The output is as follows:

\ D

['Cai8']

\ D

Matches any character other than the number, which is exactly the opposite of the \ d set. If the ASCII flag is used, it is the same set as [^ 0-9. However, using the ASCII flag affects the entire regular expression. If you only want to limit the [^ 0-9] set, it would be better to use it directly.

Example:

Print (R' \ D ')

M = re. findall (r 'cai \ d', r 'caimouse cai cai8 ')

If m:

Print (m)

The output is as follows:

\ D

['Caim ', 'cai']

\ S

In UNICODE mode:

Match UNICODE delimiter, including space, \ t \ n \ r \ f \ v, and so on.

In the 8-Bit mode:

Matches space characters, and also includes [\ t \ n \ r \ f \ v].

Example:

Print (r '\ s ')

M = re. findall (r 'cai \ s', r 'caimouse cai cai8 ')

If m:

Print (m)

The output is as follows:

\ S

['Cai ']

\ S

Match any non-space characters. This is the opposite of \ s.

Example:

Print (r '\ s ')

M = re. findall (r 'cai \ s', r 'caimouse cai cai8 ')

If m:

Print (m)

The output result is as follows:

\ S

['Caim ', 'cai8']

\ W

Matches any non-specific characters. In the 8-Bit mode, it is the same as the set [a-zA-Z0-9.

Example:

Print (R' \ W ')

M = re. findall (r 'cai \ W', r 'caimouse cai cai8 ')

If m:

Print (m)

The output is as follows:

\ W

['Caim ', 'cai8']

\ W

Match any character that does not contain any word. In 8-Bit mode, it is the same as the [^ a-zA-Z0-9] set.

Example:

Print (R' \ W ')

M = re. findall (r 'cai \ W', r 'caimouse cai cai8 ')

If m:

Print (m)

The output is as follows:

\ W

['Cai ']

\ Z

Match only at the end of the character.

Example:

Print (R' \ Z ')

M = re. findall (r 'cai \ Z', r 'caimouse \ ncai ')

If m:

Print (m)

The output result is as follows:

\ Z

['Cai ']




Cai junsheng QQ: 9073204 Shenzhen

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.