当前位置:网站首页>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
5000
3. 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'>
6
4. 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'>
6
5.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
2
Regular 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 .
边栏推荐
- 如何访问kubernetes API?
- Unity3D学习笔记4——创建Mesh高级接口
- CVPR论文解读 | 弱监督的高保真服饰模特生成
- Market Research - current situation and future development trend of marine clutch Market
- Market Research - current market situation and future development trend of marine wet exhaust hose
- [shutter] shutter gesture interaction (small ball following the movement of fingers)
- [shutter] shutter application theme (themedata | dynamic modification theme)
- :last-child 不生效解决
- Unity3D学习笔记4——创建Mesh高级接口
- Market Research - current market situation and future development trend of third-party data platform
猜你喜欢
What is it that makes you tremble? Those without fans can learn
Read a doctor, the kind that studies cows! Dr. enrollment of livestock technology group of Leuven University, milk quality monitoring
[shutter] shutter gesture interaction (small ball following the movement of fingers)
TinyMCE visual editor adds Baidu map plug-in
An overview of the development of affective computing and understanding research
The failure rate is as high as 80%. What should we do about digital transformation?
【零基础一】Navicat下载链接
How to write a good program when a big book speaks every day?
Find objects you can't see! Nankai & Wuhan University & eth proposed sinet for camouflage target detection, and the code has been open source
scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文
随机推荐
From "bronze" to "King", there are three secrets of enterprise digitalization
【零基础一】Navicat下载链接
记录一下微信、QQ、微博分享web网页功能
Market Research - current situation and future development trend of carob chocolate market
20220702-程序员如何构建知识体系?
ServiceMesh主要解决的三大痛點
【剑指 Offer】57. 和为s的两个数字
Market Research - current situation and future development trend of environmental friendly fireworks Market
图像基础概念与YUV/RGB深入理解
kubernetes资源对象介绍及常用命令(四)
[shutter] shutter layout component (wrap component | expanded component)
A week's life
The failure rate is as high as 80%. What should we do about digital transformation?
Service visibility and observability
Sql service intercepts string
Attack and defense world PWN question: Echo
How to center the positioned text horizontally and vertically
Introduction to the principle of geographical detector
B.Odd Swap Sort(Codeforces Round #771 (Div. 2))
Kubernetes resource object introduction and common commands (4)