Python Regular expression (3)--match method

Source: Internet
Author: User

1.re.match function

Re.match attempts to match a pattern from the starting position of the string, and if the match is not successful, match () returns none.

(1) function Syntax
Re.  Match(pattern,string, flags=0)       
Function parameter Description:

Pattern-matched Regular expression

String to match

Flgs flag bit, used to control how regular expressions are matched

We can use the group (NUM) or groups () matching object function to get a matching expression.

Group (num=0) gets a string of individual groupings that match the result, and group () can enter multiple group numbers at a time, returning a tuple that contains the corresponding values for those groups.

Groups () returns a tuple that contains all the grouped strings.

Note: If the mismatch succeeds, thematch() return value is None, and the group (), groups () method is used at this time to give an error.

You should get the matching object first, and then determine if the matching object is not empty, and use the group (), groups () method to get the matching result when not empty. See Example 2

(2) Example 1Import Re
Print(Re.Match(‘How‘,‘How is You‘ . Span)   # in the starting position match
    Print (re. Match ( ' are Span class= "Hl-code", "how is You ) )                # does not match the starting position

The above output results are:

  (0,3)None    
Example 2Import re content = ' 577 Zeke 'result = Re.match (R ' [a-z]\d ', content ')If result:print Result.group ()Else:print ' No match! '

The results of the above execution are as follows:
/span>

No match!

2.pattern.match method (1) Function syntax

Pattern. Match( string, pos=0, Endpos=len (String))

Function Effect:

This method will attempt to match pattern at the POS position of string strings (the pattern is the object returned after compiling through the Re.compile () method), if the pattern match succeeds, regardless of whether the end position is reached Endpos, will return a match object that matches successfully;

If the match is unsuccessful, or if the pattern does not match the end to reach Endpos, None is returned.

Function parameter Description:

String: strings that are matched

POS: The starting position of the match, optional, default is 0

Endpos: Match end position, optional, Len (string) by default

3. Re.match and Pattern.match differences

The Re.match method differs from the Pattern.match () method in that it cannot specify two parameters for a matched interval pos and Endpos

Python Regular expression (3)--match method

Related Article

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.