Python Regular expression (7)--flag modifier, Match object property

Source: Internet
Author: User
Tags aliases modifier modifiers

Regular Expressions-modifiers

A regular expression can contain some flag modifiers to control the matching pattern, which is used in the flag parameter of the regular expression handler as an optional parameter.

(1) Re. I Write All (re. IGNORECASE)

Indicates that the size is ignored when matching is made

(2) Re. M full Write (re. MULTILINE)

Multi-line matching, which affects ^ and $ behavior

(3) Re. S full Write (re. Dotall)

Make a point (.) match all characters including line breaks

(4) Re. X full Write (re. VERBOSE)

In this mode, the regular expression can be multiple lines, ignore whitespace characters, and can be added to comments.

(5) In addition to the above signs there is re. L and RE.U, but not commonly used (6) can be by using the operator "|" To specify more than one flag, indicating that it takes effect at the same time.

such as: Re. I | Re. M is set to the I and M signs,

Content = ' My username is zeke999! '

Re.search (R ' zeke\d{3} ', content, re. I | Re. M

The properties of the match object (1) string property:

Gets the string object to use when matching

>>> m = Re.match (R ' \d+ ', ' 456abc ')

>>> m.string

' 456abc '

(2) Re properties:

The pattern object used when matching, that is, the regular expression object that matches the content

>>> m

<_sre. Sre_match Object at 0x02c8fa68>

>>> m.re

<_sre. Sre_pattern Object at 0x02d4ecd0>

(3) POS Properties:

The attribute represents the index in which the text expression begins the search. The value is the same as the same name parameter in the Pattern.match () and Pattern.seach () methods

>>> M.pos

0

(4) Endpos properties:

The attribute represents the index of the end-of-search of the text expression. The value is the same as the same name parameter in the Pattern.match () and Pattern.seach () methods

>>> M.endpos

6

(5) Lastindex properties:

The attribute represents the index of the last captured grouping in the text. If there are no captured groupings, it will be none

>>> m = Re.match (R ' A (b) (c) d ', ' abcdef ')

>>> M.lastindex

2

(6) Lastgroup properties:

The attribute represents the last group alias that was captured. If the group has no aliases or no captured groupings, it will be none.

(7) Group ([Group1, ...]) :

Gets the string that is intercepted by one or more groups, and returns a tuple when multiple parameters are specified. Group1 can use numbers or aliases; number 0 represents the entire substring of the match; default returns Group (0)

Example: Group function passes multiple parameters

p = re.compile (' (A (b) c) d ')

m = P.match (' ABCD ')

Restup = M.group (1,2,1)

Print Restup

>>> (' abc ', ' B ', ' abc ')

(8) groups ([Default=none])

Returns the string intercepted by all groups as a tuple. Equivalent to calling group (,... last)

(9) Start ([Group=0])

Returns the starting index of the substring intercepted by the specified group in string (the index of the first character of the substring). The default is group No. 0, which is the entire string

(+) End ([Group=0])

Returns the end index (the index of the last character of the substring) of the specified group of intercepted substrings in a string. The group default value is 0, which is the entire string

(one) span ([group])

The method represents the return as a tuple (start (group), End (group)), that is, the matched text content of a group at the beginning and end of the matched string at the index position

(expand) (template)

Substituting the matched grouping into the template and then returns. The template can be grouped using \id or \g<id>, \g<name> reference, but cannot use number 0. \id and \g<id> are equivalent, but \10 will be considered a 10th grouping, if you want to express \1 after the character ' 0 ', use only \g<1>0.

m = Re.search (R ' (\w+)! (\w+) (\w+) ', ' hman! How finny! ') #将匹配的结果带入 print M.expand (R ' resut:\3 \2 \1 ')

>>> Resut:finny How HMan

(groupdict) ([Default=none])

The function is to assign all the groupings that match to and specify the alias, alias as key, match the string to value, save in the dictionary, and return to the dictionary. If an alias group is not set in an expression, an empty dictionary is returned

>>> m = Re.search (R ' (? p<num>\d+) (\w+) ', ' 78FD ')

>>> m.groupdict ()

{' num ': ' 78 '}

Python Regular expression (7)--flag modifier, Match object property

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.