Python Regular expression (8)--grouping, back reference, front (back) assertion

Source: Internet
Author: User

Nameless, well-known group (1) Regular expression-nameless group

Starting from the left side of the regular expression, the first opening parenthesis "(" represents the first grouping, the second represents the second grouping, and so on.

It is important to note that there is an implied global grouping (that is, the grouping with index number 0), which is the result of the entire regular expression match.

(2) Regular expressions-well-known groupings

A named Group is another alias for a group that has a default grouping number, which facilitates subsequent references. The syntax format for command grouping is as follows: (? p<name> Regular expression)

The character p in the syntax format must be an uppercase "P", and name is a valid identifier that represents the alias of the grouping.

s = "ip= ' 230.192.168.78 ', version= ' 1.0.0 '"

res = Re.search (r "ip=" (? p<ip>\d+\.\d+\.\d+\.\d+). * ", s)

Print res.group (' IP ') #通过命名分组引用分组

Regular expression-back reference

When a regular expression grouping is defined with "()", the regular engine will number the matched groups sequentially and then store them in the cache. This allows us to refer back to what has been matched, which is called a back reference.

(1) referencing by index

The \ Number \1 refers to the first grouping, \2 refers to the second grouping, and so on, \ n refers to the nth group, while the-n represents the entire matched regular expression itself.

Swap the position of a string

Import re

s = ' abc.xyz ' # Interchange. String on both sides of the number

res = Re.sub (R ' (. *) \. (. *) ', R ' \2.\1 ', s)

Print Res

>>>xyz.abc

(2) (? P=name) Reference by naming the group name

(? P=name) character P must be uppercase P,name represents the group name of the named group

(? p<name>) (? P=name) The value matching value of a reference grouping must be equal to the first grouping match value to match to

For example:

1) Refer to the previous group, the value of the same is 2, it can match to

>>> Re.match (R ' (? p<xst>\d) (? P=XST) ', ' (') '. Groups ()

(' 2 ',)

>>> Re.match (R ' (? p<xst>\d) (? P=XST) ', ' (') '). Group ()

' 22 '

2) referring to the previous group, the values are not the same as 2 and 3 respectively, so it cannot be matched to

>>> Re.match (R ' (? p<xst>\d) (? P=XST) ', ' (') '). Group ()

Traceback (most recent):

File "<stdin>", line 1, in <module>

Attributeerror: ' Nonetype ' object has no attribute ' group '

Regular expression-The syntax of Forward assertion (1) forward positive assertion and backward positive assertion 1) positive assertion:

(? <=pattern) A positive assertion indicates that you want the matched string to match before it matches the pattern.

2) The syntax of the post-affirmation assertion:

(? =pattern) After a positive assertion indicates that you want the matched string to match after the pattern matches the content.

3) Use both forward and backward assertions

If you need to use both a forward positive assertion and a back-positive assertion in a single match, you must write the forward positive assertion expression before the regular expression you want to match, and then write the positive assertion expression after the string you want to match.

(2) The syntax of the forward negation assertion and the back Negation assertion 1):

(? <!pattern) A forward negative assertion indicates that you want the matched string to match before it is not a pattern-matched content.

2) syntax for a back-to-negative assertion:

(?! pattern) The back-to-negative assertion indicates that you want the matched string to match when it is not followed by a pattern match.

(3) Note

Forward positive (negative) assertion that the regular expression in parentheses must be a regular expression that can determine the length, such as \w{3}, and not be written as \w* or \w+ or \w.

Python Regular expression (8)--grouping, back reference, front (back) assertion

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.