Use of Regular Expressions in python, python Regular Expressions
This article describes the symbols and methods of python regular expressions.
Findall: searches for all matches and returns the list of all combinations.
Search: searches for the first match and returns
Sub: Replace the regular content and return the replaced content.
.: Match any character except line breaks
a = 'xy123'b = re.findall('x...',a)print(b)# ['xy12']
*: Match the first character 0 times or unlimited times
a = 'xyxy123'b = re.findall('x*',a)print(b)# ['x', '', 'x', '', '', '', '', '']
? : Match the first character 0 times or 1 time
a = 'xy123'b = re.findall('x?',a)print(b)# ['x', '', '', '', '', '']
. *: Greedy algorithm
b = re.findall('xx.*xx',secret_code)print(b)# ['xxIxxfasdjifja134xxlovexx23345sdfxxyouxx']
.*? : Non-Greedy Algorithm
c = re.findall('xx.*?xx',secret_code)print(c)# ['xxIxx', 'xxlovexx', 'xxyouxx']
(): Returns results in parentheses.
d = re.findall('xx(.*?)xx',secret_code)print(d)for each in d: print(each)# ['I', 'love', 'you']# I# love# you
Re. S makes the. Scope include the line break "\ n"
s = '''sdfxxhelloxxfsdfxxworldxxasdf'''d = re.findall('xx(.*?)xx',s,re.S)print(d)# ['hello\n', 'world']
Comparison between findall and search
s2 = 'asdfxxIxx123xxlovexxdfd'f = re.search('xx(.*?)xx123xx(.*?)xx',s2).group(2)print(f)f2 = re.findall('xx(.*?)xx123xx(.*?)xx',s2)print(f2[0][1])# love# love
Although the results are the same, search matches the group to obtain the second match, and findall Returns [('I', 'love')], containing the list of tuples, therefore, f2 [0] [1] is required.
Use of sub
s = '123rrrrr123'output = re.sub('123(.*?)123','123%d123'%789,s)print(output)# 123789123
For example, we need to change the path of all png images in the document, that is, we need to find the end of all. png images and add them to the path,
import redef multiply(m): # Convert group 0 to an integer. v = m.group(0) print(v) # Multiply integer by 2. # ... Convert back into string and return it. print('basic/'+v) return 'basic/'+v
The result is as follows:
>>>autoencoder.png basic/autoencoder.png RNN.png basic/RNN.png rnn_step_forward.png basic/rnn_step_forward.png rnns.png basic/rnns.png rnn_cell_backprop.png basic/rnn_cell_backprop.png LSTM.png basic/LSTM.png LSTM_rnn.png basic/LSTM_rnn.png attn_mechanism.png basic/attn_mechanism.png attn_model.png basic/attn_model.png
Following the examples above, we can easily customize our tasks.
Compared with sub, subn returns a tuples. The second element indicates the number of times that a replacement occurs:
import redef add(m): # Convert. v = int(m.group(0)) # Add 2. return str(v + 1)# Call re.subn.result = re.subn("\d+", add, "1 2 3 4 5")print("Result string:", result[0])print("Number of substitutions:", result[1])>>>Result string: 11 21 31 41 51Number of substitutions: 5