当前位置:网站首页>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 .
边栏推荐
- Redis distributed lock failure, I can't help but want to burst
- Une semaine de vie
- [shutter] shutter opens a third-party application (url|launcher plug-in search and installation | url| launcher plug-in official example | open browser | open a third-party application)
- Les trois principaux points de douleur traités par servicemesh
- Blue Bridge Cup Eliminate last one (bit operation, code completion)
- [shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)
- [Yu Yue education] reference materials of analog electronic technology of Nanjing Institute of information technology
- VictoriaMetrics 简介
- 100 important knowledge points that SQL must master: using cursors
- How to prevent your jar from being decompiled?
猜你喜欢

Share how to make professional hand drawn electronic maps
![[zero foundation I] Navicat download link](/img/23/e7808884152eeaf186fe756d6adac6.png)
[zero foundation I] Navicat download link

Ransack combined condition search implementation

Blue Bridge Cup Winter vacation homework (DFS backtracking + pruning)

Introduction to victoriametrics

Landingsite eband B1 smoke test case

LightGBM原理及天文数据中的应用
![[shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)](/img/fa/5c1b6c16d9aabd13e9a4f7c7b9c7da.jpg)
[shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)

CVPR论文解读 | 弱监督的高保真服饰模特生成

Lightgbm principle and its application in astronomical data
随机推荐
The difference between include < > and include ""
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
[shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
地理探测器原理介绍
Oriental Aesthetics and software design
Learn computer knowledge from scratch
Browser - clean up the cache of JS in the page
[sword finger offer] 56 - I. the number of numbers in the array
[Jianzhi offer] 56 - ii Number of occurrences of numbers in the array II
Centos7 installation and configuration of redis database
如何访问kubernetes API?
Unity3d learning notes 4 - create mesh advanced interface
攻防世界pwn题:Recho
D4: unpaired image defogging, self enhancement method based on density and depth decomposition (CVPR 2022)
[shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)
Technological Entrepreneurship: failure is not success, but reflection is
Micro service gateway selection, please accept my knees!
Market Research - current market situation and future development trend of aircraft front wheel steering system
An overview of the development of affective computing and understanding research
Secondary development of ANSYS APDL: post processing uses command flow to analyze the result file