Python serialization 49 regular examples

Time:2020-2-23

1、 Re example

 

import re

#Find numbers

p = re.compile(r"\d+")

#In the string "ongahjeuf125" and retrograde search, search according to the rule specified in rule p

M = p.match ("ong125fdsgdsf48515", 3,20) 񖓿 search range of parameter No. 3 to 6

print(m)

#Above code description

#1. Match can input parameters to indicate the starting position

#2. Only one result can be found, which indicates that the first matching is successful

Print (m [0]) directly prints out the matching content

Print (m.start (0)) ා񖓿񖓿where the matching content starts

Print (m.end (0)) ා񖓿񖓿ාwhere the matching content ends

2. Another example: content can be obtained in groups

#In the parameter, re. I means ignore case

p1 = re.compile(r"([a-z]+) ([a-z]+)",re.I)

m1 = p1.match("I am relly good man")

print(m1)

print(m1.group(0))

print(m1.start(0))

print(m1.end(0))

print("===================")

print(m1.group(1))

print(m1.start(1))

print(m1.end(1))

print("===================")

print(m1.group(2))

print(m1.start(2))

print(m1.end(2))

print("===================")

print(m1.groups())

3. search

(1) Search (STR, POS [, endpox]]): find a match in the string, POS and endpos represent the starting position

(2) Findall: find all

(3) Finder: find, return an ITER result

4. replacement

sub(rep1,str[,count])

 

p2 = re.compile(r"\d+")

m2 = p2.search("one12two34three567four")

print(m2.group())

m3 = p2.findall("one12two34three567four")

print(type(m3))

print(m3)

​

p3 = re.compile(r"(\w+) (\w+)")

s = "hello 123 wang 456 jfowe,i jodf "

ret = p3.sub(r"Hello world",s)

print(ret)

5. pay attention to

Most Chinese content is represented in [u4e00-u9fa5], excluding full angle punctuation

Title = "Hello world, hello moto"

p4 = re.compile(r"[\u4e00-\u9fa5]+")

rst = p4.findall(title)

print(rst)

Two, source code

D31_1_RexLearning.py

https://github.com/ruigege66/Python_learning/blob/master/D31_1_RexLearning.py​

2. CSDN: https://blog.csdn.net/weixin_

3. Blog Park: https://www.cnblogs.com/ruige0000/

4. welcome to WeChat official account: Fourier transform, official account number, only for learning communication, background reply, “gift package”, get big data learning materials.