Python full stack regular expression (re module regular interface full explanation)

Source: Internet
Author: User
Tags first string

The RE module is the standard library module for Python

The overall mode of the module regular interface

Re.compile return Regetx Object

Finditer Fullmatch Match Search returns the match object

Match. Properties | method

Use of the RE module:

Regex = re.compile (pattern,flags = 0)

Function:

Generating a regular Expression object

Parameters:

Pattern Regular Expression

Flags function flag bit, rich regular expression matching

return value:

Returns a regular Expression object

Re.findall (pattern,string,flags = 0)

Function:

Match target string contents according to regular expression

Parameters:

Pattern Regular Expression

String target strings

return value:

The list is matched to the content

If the regular expression has subgroups, only the content in the subgroup is returned

Regex.findall (String,pos,endpos)

Function:

Match target string contents according to regular expression

Parameters:

String target strings

Pos,endpos: Intercept the starting and ending positions of the target string to match, default is the entire string

return value:

The list is matched to the content

If the regular expression has subgroups, only the content in the subgroup is returned

Re.split (pattern,string,flags = 0)

Function:

Cutting the target string with regular expressions

Parameters:

Pattern Regular

String target strings

return value:

Return the cut content as a list

Re.sub (Pattern,replace,string,max,flags)

Function:

Replace regular expression match content

Parameters:

Pattern Regular

Replace the content to be replaced

String target strings

Max settings Replace several places

return value:

The replaced string

RE.SUBN (Pattern,replace,string,max,flags)

Functions and parameters with sub

Return value more one actually replaced several places

Re.finditer (Pattern,string,flags)

Function:

Using regular matching target strings

Parameters:

Pattern Regular

String target strings

return value:

Iteration Object----Iteration content as Match object

Re.fullmatch (Pattern,string,flags)

Function:

Exactly matches a string

Parameters:

Pattern Regular

String target strings

return value:

Match object, matching to the content

Re.match (Pattern,string,flags)

Function:

Match a string start content

Parameters:

Pattern Regular

String target strings

return value:

Match object, matching to the content

Re.search (Pattern,string,flags)

Function:

Match the first string that matches a condition

Parameters:

Pattern Regular

String target strings

return value:

Match object, matching to the content

Properties of the Regex object

Flags flag digit Value

Pattern Regular Expression

Groups number of sub-groups

Groupindex Gets the Capture group dictionary, the key for the group name value is the first group

Match Object properties:

Match.string indicates the starting position of the target string

Match.pos indicates the end position of the target string

Match.re indicates that an object generates a regular expression

Match.endpos Target String

Match.lastindex the last grouping is the first group

Match.lastgroup name of the last group (capture)

Match Object method:

Match.span () returns a tuple that matches the start end of the content

Match.start () returns the start position of the match to the content

Match.end () Returns the end position of the match to the content

Match.Groups () returns the content to which all subgroups match

Match.groupdict () returns the capture group Dictionary key: capture Name Value: Content

Group (n=0)

Function:

Gets the match object corresponding to the matching content

Parameters:

The default is 0 to get the whole match

If you assign a value of ... Represents getting the nth sub-group match to the content

return value:

Returns the retrieved content string

#regex1.pyImportRepattern= R"(? P<DOG>AB) CD (? P<PIG>EF)"#generating a regular expression objectRegex =Re.compile (pattern) s="ABCDEFGHFKFDAFSABCDEFJSAAVJHCABCA"#Get Mtach Objectobj = Regex.search (s, 0, 8)#set start position end position#print (len (s))#Match Object PropertiesPrint(Obj.pos)#the starting position of the target stringPrint(Obj.endpos)#end position of the target stringPrint(obj.re)#the regular Expression object Re.compile (' (? P<DOG>AB) CD (EF) ')Print(obj.string)#Target StringPrint(Obj.lastindex)#the last grouping is the first groupPrint(Obj.lastgroup)#the name of the last group#Match Object MethodPrint(Obj.span ())#match the beginning and end of the contentPrint(Obj.start ())#match to the beginning of the contentPrint(Obj.end ())#match to the end of the content locationPrint(Obj.groups ())#what all subgroups match toPrint(Obj.groupdict ())#Capture Group Dictionary key: capture Name value: ContentPrint(Obj.group ())Print(Obj.group (2))#Group (n=0)#Features:#gets the match object corresponding to the matching content#Parameters:#The default is 0 to get the whole match#If you assign a value of ... Represents getting the nth sub-group match to the content#return Value:#returns the retrieved content string

Flags parameter:

Re.compile

Re.findall

Re.search

Re.match

Re.finditer

Re.fullmatch

Re.split

Re.sub

Role:

Auxiliary regular expressions, extending rich matching content,

Regex = Re.compile (r "Hello", re.) I) # Ignore letter case

I = = IGNORECASE Ignore letter case

S = = Dotall let metacharacters. can match to \ n

M = = MULTILINE Let meta-character ^ $ be able to match the beginning and end of each line

X = = Verboos can add annotations to the regular

Flags can be used with a bitwise or when passing multiple arguments: | Link

ImportRe#Ignore letter CaseRegex = Re.compile (r'Hello', Re. I)#L = regex.findall (' Hello hello ')#print (L)s=" "Hello Worldnihao Beijing" "#let. Be able to match line breaksL = Re.findall (r'.+', S,re. S)Print(L)#match each rowobj = Re.search (r"world$", S,re. M)Print(Obj.group ())#re Self-explanatory methodPattern = R"""(? p<dog>\w+) #dog组 \s+ #匹配任意多个空格 (\w+) #匹配一些特殊字符"""#Add comments Ignoring cases = Re.match (pattern,'Hello%#@', Re. X |Re. I). Group ()Print(s)

Python full stack regular expression (re module regular interface full explanation)

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.