想要把那些正则表达式的东西给学会, 就直接去看下面这些个例子就行了,总共有20个呢, 这还是一个经过脚本之家这个团队修正过的版本。
更新时间被设定在了公元2020年的03月03日那一天的23:45:37, 文章的作者是算法。
这篇文章主要介绍了学会正则表达式, 就看这20个例子, 需要的朋友可以参考下。
大家普遍都觉得, 正则表达式在处理字符串的时候是非常管用的一个工具。作为一个理论概念来说, 正则表达式这个东西并不是某一种编程语言或者软件环境所独有的。不过呢, 在具体的使用过程里面, 各个平台之间的正则表达式在实际操作时还是存在某些细微的差异情况的。
正则表达式是一种特殊的字符序列, 它是可以帮助你检查一个字符串是否和某种模式相符合的那一种。
从一点五版开始, 咱们新加了一个叫 re 的模块, 这个里头提供了那种类似 Perl 里的正则表达式的模式。
该re模块可以赋予编程语言完整的能力, 从而使得这种语言具备处理所有正则表达式的功能。
这函数会用一个模式字符串, 再加上可选的一些标志参数, 去生成出一个正则表达式对象出来的, 那个对象里面包含着一序列的方法, 可以用来进行正则表达式的匹配和替换处理的。
那个叫做 re 的模块里头, 还提供出了另外一些函数, 这些函数的功能, 是完全一样的, 和前面提到的那些方法功能一致, 这些函数在调用的时候, 是要用上一个模式字符串的, 并且把这个模式字符串放在第一个参数的位置。
1、查找第一个匹配串
import re
s='i love python very much'
pat='python'
r=re.search(pat,s)
print(r.span())#(7,13)
2、查找所有1
import re
s='山东省潍坊市青州第1中学高三1班'
pat='1'
r=re.finditer(pat,s)
for i in r:
print(i)
#
#
3、\d匹配数字
import re
s='一共20行代码运行时间13.59s'
pat=r'\d+'#+表示匹配数字(\d表示数字的通用字符)1次或多次
r=re.findall(pat,s)
print(r)
#['20','13','59']
我们这边的意思就是, 想把13.59这个数值给留下来, 别把它拆开去处理, 具体的情况请大家去看一下那个第4点。
4、那个问号它表示说前一个字符可以匹配上零次也可以匹配上一次。
import re
s='一共20行代码运行时间13.59s'
pat=r'\d+\.?\d+'#?表示匹配小数点(\.)0次或1次
r=re.findall(pat,s)
print(r)
#['20','13.59']
5、这个匹配字符串的开头。
import re
s='This module provides regular expression matching operations similar to those found in Perl'
pat=r'^[emrt]' #查找以
r=re.findall(pat,s)
print(r)
# [],因为字符串的开头是字符`T`,不在emrt匹配范围内,所以返回为空
6、关于, re.I这个标志表示忽略大小写。
import re
s='This module provides regular expression matching operations similar to those found in Perl'
pat=r'^[emrt]' #查找以
r=re.compile(pat,re.I).search(s)
print(r)
# 表明字符串的开头在匹配列表中
7、使用正则提取单词
这个版本是不对的, 你赶紧去看第九个。
import re
s='This module provides regular expression matching operations similar to those found in Perl'
pat=r'\s[a-zA-Z]+'
r=re.findall(pat,s)
print(r) #[' module', ' provides', ' regular', ' expression', ' matching', ' operations', ' similar', ' to', ' those', ' found', ' in', ' Perl']
8、仅仅进行单词的捕获动作, 同时将空格予以去除掉。
通过使用括号来进行捕获, 这是一个不准确的版本, 请大家参考一下第9个。
import re
s='This module provides regular expression matching operations similar to those found in Perl'
pat=r'\s([a-zA-Z]+)'
r=re.findall(pat,s)
print(r)
#['module', 'provides', 'regular', 'expression', 'matching', 'operations', 'similar', 'to', 'those', 'found', 'in', 'Perl']
9、把之前提到的那一个词眼再次给它加上去。
在上面看到的第8点里面, 提到一个情况, 也就是在提取单词的时候, 第一个单词没有被包括进去。这里的情况是使用了问号来表示前面的那个字符, 能够出现0次或者1次。不过, 这个字符它除此之外, 还表示了有贪婪匹配或者非贪婪匹配的某种含义。所以, 在使用它的时候, 大家一定要小心谨慎。
import re
s='This module provides regular expression matching operations similar to those found in Perl'
pat=r'\s?
([a-zA-Z]+)'
r=re.findall(pat,s)
print(r)
#['This', 'module', 'provides', 'regular', 'expression', 'matching', 'operations', 'similar', 'to', 'those', 'found', 'in', 'Perl']
10、在使用split这个函数的时候, 可以直接进行单词的分割操作。
使用上面的那些办法来把单词给切碎掉, 其实并不算得上是简洁的做法, 这么做仅仅就是为了做一个展示的例子而已。事实上, 要把单词进行分割,最为简单的操作还是直接使用那个被称为 split 的函数来进行处理就可以了。
import re
s = 'This module provides regular expression matching operations similar to those found in Perl'
pat = r'\s+'
r = re.split(pat,s)
print(r)
#['This', 'module', 'provides', 'regular', 'expression', 'matching', 'operations', 'similar', 'to', 'those', 'found', 'in', 'Perl']
11、把那些开头字母是m或者说是t的单词给提取出来, 然后在这个过程中呢, 完全不用去管它的大小写形式怎么样。
下面出来的这些个结果, 并不是我们所期待看到的那种样子。这个状况的发生, 原因是出在问号上面的那个位置上的!
import re
s='This module provides regular expression matching operations similar to those found in Perl'
pat=r'\s?
([mt][a-zA-Z]*)' # 查找以
r=re.findall(pat,s)
print(r)
#['module', 'matching', 'tions', 'milar', 'to', 'those']
12、使用^查找字符串开头的单词
把11和12综合起来, 这样可以得到所有开头是m或者t的单词。
import re
s='This module provides regular expression matching operations similar to those found in Perl'
pat=r'^([mt][a-zA-Z]*)\s' # 查找以
r=re.compile(pat,re.I).findall(s)
print(r)
#['This']
13、先把东西给它分开来, 然后再去把那一些是符合规定的单词找出来。
你可以使用match这个标识来展示是否符合匹配的状态。
import re
s='This module provides regular expression matching operations similar to those found in Perl'
pat=r'\s+'
r=re.split(pat,s)
res=[i for i in r if re.match(r'[mMtT]',i)]
print(res)
#['This', 'module', 'matching', 'to', 'those']
14、贪心匹配
尽可能多的匹配字符
import re
content='ddedadsad
bb
cc'
pat=re.compile(r"
") #贪婪模式
m=pat.findall(content)
print(m)
#['graphbb
15、非贪心匹配
跟十四相对照的话, 它们之间的差别, 其实就是仅仅多出来一个问号这个符号, 而因为这个差异的存在, 所以最终所得出来的结果, 是完全不一样的情况。
import re
content='ddedadsad
bb
cc'
pat=re.compile(r"
") #贪婪模式
m=pat.findall(content)
print(m)
#['graph', 'math']
把这两者拿来跟第14条进行比较, 就可以知道贪心匹配和非贪心匹配之间的区别是什么, 后面的这个后者是在完成了字符串的匹配动作之后, 立马就直接进行返回操作, 它体现出来的就是一个见好就收的状态。
16、含有多种分割符
使用split函数
import re
content = 'graph math,,english;chemistry' #这种
pat=re.compile(r"[\s\,\;]+") #贪婪模式
m=pat.split(content)
print(m)
#['graph', 'math', 'english', 'chemistry']
17、把已经匹配到的那些子串, 进行替换。
sub函数实现对匹配子串的替换
import re
content="hello 12345, hello 456321"
pat=re.compile(r'\d+') #要替换的部分
m=pat.sub("666",content)
print(m)
#hello 666, hello 666