当前位置:网站首页>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 .
边栏推荐
- Market Research - current market situation and future development trend of genome editing mutation detection kit
- Servicemesh mainly solves three pain points
- Promise optimized callback hell
- The difference between include < > and include ""
- 图像基础概念与YUV/RGB深入理解
- PIP version update timeout - download using domestic image
- Unity3D学习笔记4——创建Mesh高级接口
- : last child does not take effect
- Reading experience of just because
- The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"
猜你喜欢
TinyMCE visual editor adds Baidu map plug-in
【零基础一】Navicat下载链接
Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
[shutter] shutter application theme (themedata | dynamic modification theme)
The difference between include < > and include ""
Lightgbm principle and its application in astronomical data
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
C语言,实现三子棋小游戏
Introduction to the principle of geographical detector
Infrastructure is code: a change is coming
随机推荐
关于PHP-数据库的 数据读取,Trying to get property 'num_rows' of non-object?
"Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks!
Error in PIP installation WHL file: error: is not a supported wheel on this platform
D4: unpaired image defogging, self enhancement method based on density and depth decomposition (CVPR 2022)
Market Research - current market situation and future development trend of third-party data platform
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)
Bridge emqx cloud data to AWS IOT through the public network
How do I access the kubernetes API?
Promise optimized callback hell
From "bronze" to "King", there are three secrets of enterprise digitalization
Introduction to victoriametrics
Browser - clean up the cache of JS in the page
Pip install whl file Error: Error: … Ce n'est pas une roue supportée sur cette plateforme
Market Research - current market situation and future development trend of high tibial osteotomy plate
Destroy in beforedestroy invalid value in localstorage
pip安装whl文件报错:ERROR: ... is not a supported wheel on this platform
Ransack组合条件搜索实现
Ransack combined condition search implementation
[shutter] shutter gesture interaction (small ball following the movement of fingers)
kubernetes资源对象介绍及常用命令(四)