当前位置:网站首页>Regular expression (2)
Regular expression (2)
2022-07-02 22:25:00 【Bomapple】
re modular ( Built-in module )
re The module is python Provides a set of modules for processing regular expressions . The core functions are the first four , Other functions are the last four :
1. findall lookup character string all Relevant Character set . return list
lst = re.findall(r"m", "my mother, I love him!")
print(lst)
lst = re.findall(r"\d+", "6 Before the point . I kidnapped 12 Children at the age of , You're going to give it to me 5000 ten thousand ")
print(lst)['m', 'm', 'm']
['6', '12', '5000']2. finditer and findall almost . It's just that what comes back is iterator ,
Extremely efficient , In the days to come , Reptiles are also good , Data cleaning is also good , I hope to choose this first finditer
iters = re.finditer("\d+", "6 Before the point . I kidnapped 12 Children at the age of , You're going to give it to me 5000 ten thousand ")
for it in iters:
print(it.group()) # You still need to group 6
12
50003. search On the string One match . But if it matches The first result . will Return this result . If it doesn't match search The one who returns is None.
ret = re.search(r"\d+", "6 Before the point . I kidnapped 12 Children at the age of , You're going to give it to me 5000 ten thousand ")
print(ret)# Return the object class , You need to use the instance method to get the value
print(ret.group())# use group Instance method can take out the attribute <re.Match object; span=(0, 1), match='6'>
64. match Only from the start Conduct matching .( Be similar to Regular expression metacharacters ^ Matches the beginning of the string )
ret = re.match(r"\d+", "6 Before the point . I kidnapped 12 Children at the age of , You're going to give it to me 5000 ten thousand ")
print(ret)# Return the object class , You need to use the instance method to get the value
print(ret.group())# use group Instance method can take out the attribute <re.Match object; span=(0, 1), match='6'>
65.split On the string cutting .( It's similar to cutting meat with a knife , Metacharacters are knives , Meat string )
ret = re.split('[a-l]', 'Andy has a apple,but she not have banana')# from a-l take 12 A different knife , Cut the meat , Put it into the list after cutting , Take the knife back
print(ret)['An', 'y ', '', 's ', ' ', 'pp', '', ',', 'ut s', '', ' not ', '', 'v', ' ', '', 'n', 'n', '']6.sub On the string Replace .( Similar to... In string processing replace, But he low,sub Than him high That's too much !)
ret=re.sub(r"\d+",'big_sb',' My name is 123, This year, 12 year , From 12 Middle school ')# You can change all the figures into big pancakes
print(ret) My name is big_sb, This year, big_sb year , From big_sb Middle school 7.subn On the string Replace . And return tuples ( Replaced string , Number of replacements )
ret=re.subn(r"\d+",'big_sb',' My name is 123, This year, 12 year , From 12 Middle school ')
print(ret)(' My name is big_sb, This year, big_sb year , From big_sb Middle school ', 3)8.complie Preprocessing strings .( It is equivalent to setting some rule of regular expression on the string first , Then filter the string )
obj = re.compile(r'\d+') # Compile the regular expression into a Regular expression objects , The rule is to match all the numbers
ret = obj.finditer('ewq123e2ee2e') # Regular expression object calls finditer, The parameter is the string to be matched
for i in ret:
print(ret.group())123
2
2Regular expressions and re There are so many modules . If you want to explain all the contents of regular clearly , At least a week . For our daily use . The above knowledge points are enough . If you encounter some extreme situations, it is recommended to find ways to deal with them separately . First split the string . Then consider using regular .
边栏推荐
- Necessary browser plug-ins for network security engineers
- The difference between include < > and include ""
- "New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
- 【剑指 Offer】57. 和为s的两个数字
- Attack and defense world PWN question: Echo
- Ransack组合条件搜索实现
- [sword finger offer] 56 - I. the number of numbers in the array
- [C question set] of V
- Daily book -- analyze the pain points of software automation from simple to deep
- Oriental Aesthetics and software design
猜你喜欢

From personal heroes to versatile developers, the era of programmer 3.0 is coming

*C language final course design * -- address book management system (complete project + source code + detailed notes)
![[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)](/img/4c/c8dae41fc2eb18b5153cf36861fc7d.jpg)
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)

The difference between include < > and include ""

Technical solution of vision and manipulator calibration system
![[staff] Sibelius 7.5.1 score software installation (software download | software installation)](/img/1a/4932a7931c54248c065cf8a1462d34.jpg)
[staff] Sibelius 7.5.1 score software installation (software download | software installation)

Interpretation of CVPR paper | generation of high fidelity fashion models with weak supervision

Official announcement! The golden decade of new programmers and developers was officially released

About test cases

"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
随机推荐
Interpretation of CVPR paper | generation of high fidelity fashion models with weak supervision
Hanoi Tower problem
A week's life
关于PHP-数据库的 数据读取,Trying to get property 'num_rows' of non-object?
[leetcode] sword finger offer 11 Rotate the minimum number of the array
Web侧防御指南
An overview of the development of affective computing and understanding research
分享一下如何制作专业的手绘电子地图
Record the functions of sharing web pages on wechat, QQ and Weibo
图像基础概念与YUV/RGB深入理解
Try to get property'num for PHP database data reading_ rows' of non-object?
Attack and defense world PWN question: Echo
Etcd raft protocol
关于测试用例
Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
SQL必需掌握的100个重要知识点:使用游标
Secondary development of ANSYS APDL: post processing uses command flow to analyze the result file
Five message formats of OSPF
Gee: (II) resampling the image
pyqt图片解码 编码后加载图片