[Python] lists the main methods used by python and regular re.
[Code directly]
# Coding = UTF-8
#1. First, compile the regular expression string form into a Pattern instance
#2. Use the Pattern instance to process text and obtain matching results
#3. Use the Match instance to obtain the message and perform other operations.
Import re
# [1] re. compile (string [, flag]) compile the regular expression into a pattern object
'''
# [2] re. match (pattern, string [, flags])
# Try to match pattern from the beginning of the input string,
# If a character that cannot be matched or has reached the end of the string, "None" is returned immediately. Otherwise, the matching result is obtained.
# Compile a regular expression into a pattern object
Pattern = re. compile (R' \ d + ')
# Use re. match to match the text to obtain the matching result. If the matching fails, None is returned.
Result1 = re. match (pattern, '192abc ')
If result1:
Print result1.group ()
Else:
Print 'matching failed 1'
Result2 = re. match (pattern, 'abc192 ')
If result2:
Print result2.group ()
Else:
Print 'matching failed 2'
# Output result --------> 192 match failed 2 resul2 match failed when a is encountered, and None is returned immediately
'''
'''
# [3] re. search (pattern, string [, flags])
# The match () function only matches the string position.
# Search () scans the entire string for matching
# Compile a regular expression into a pattern object
Pattern = re. compile (R' \ d + ')
# Use re. search to match the text to obtain the matching result. If the match fails, None is returned.
Result1 = re. search (pattern, 'abc192def ')
If result1:
Print result1.group ()
Else:
Print 'matching failed 1'
# Running result --------> 192
'''
'''
# [4] re. split (pattern, string [, maxsplit])
# Split string by matching substrings and return to the list
# Maxsplit is used to specify the maximum number of splits. If this parameter is not specified, all splits.
Pattern = re. compile (R' \ d + ')
Print re. split (pattern, 'a1b2c3d4 ')
# Running result -----------> ['A', 'B', 'C', 'D', '']
'''
'''
# [5] re. findall (pattern, string [, flags])
# Search for the entire string and return all matched substrings in the form of a list
Pattern = re. compile (R' \ d + ')
Print re. findall (pattern, 'a1b2c3d4 ')
# Running result ----------> ['1', '2', '3', '4']
'''
'''
# [6] re. finditer (pattern, string [, flags])
# Search for the entire string and return all matching objects in the form of an iterator
Pattern = re. compile (R' \ d + ')
Matchiter = re. finditer (pattern, 'a1b2c3d4 ')
For match in matchiter:
Print match. group ()
# Running result ------>
#1
#2
#3
#4
'''
'''
# [7] re. sub (pattern, repl, string [, count])
# Use repl to replace each matched substring in the string and then return the replaced string.
# When repl is a string, you can use \ id, \ g <id>, \ g <name> to reference the group, but cannot use number 0.
# When repl is a method, this method should only accept one parameter (Match object) and return a string for replacement (the returned string cannot reference the Group)
# Count is used to specify the maximum number of replicas. If not specified, all replicas are replaced.
P = re. compile (R '(? P <word1> \ w + )(? P <word2> \ w +) ') # reference by name
S = 'I say, hello world! '
Print p. sub (R' \ g <word2> \ g <word1> ', s)
P = re. compile (R' (\ w +) ') # Use Id
Print p. sub (R' \ 2 \ 1 ', s)
Def func (m ):
Return m. group (1). title () + ''+ m. group (2). title ()
Print p. sub (func, s)
# Output result ------>
# Say I, world hello!
# Say I, world hello!
# I Say, Hello World!
'''
# [8] re. subn (pattern, string [, count])
# Return (sub (repl, string [, count]), replacement times ).
S = 'I say, hello world! '
P = re. compile (R' (\ w + )')
Print p. subn (R' \ 2 \ 1', s)
Def func (m ):
Return m. group (1). title () + ''+ m. group (2). title ()
Print p. subn (func, s)
# Running result -------->
# ('Say I, world hello! ', 2)
# ('I Say, Hello World! ', 2)