2023年12月14日发(作者:)
【python】re模块的用法(1)——基本语法
Python 的 re 模块(Regular Expression 正则表达式)提供各种正则表达式的匹配操作,在文本解析、复杂字符串分析和信息提取时是
一个非常有用的工具,下面我主要总结了re的常用方法。
的简介
使用python的re模块,尽管不能满足所有复杂的匹配情况,但足够在绝大多数情况下能够有效地实现对复杂字符串的分析并提取出相关
信息。python 会将正则表达式转化为字节码,利用 C 语言的匹配引擎进行深度优先的匹配。
1. import re
2. print re.__doc__
可以查询re模块的功能信息,下面会结合几个例子说明。
的正则表达式语法
正则表达式语法表如下:
语法
"."
"^"
"$"
"*"
"+"
"?"
*?,+?,??
{m,n}
{m,n}?
""
[]
"|"
(...)
(?#...)
(?=...)
(?!...)
(?<=...)
(?
意义
任意字符
字符串开始
字符串结尾
0 个或多个字符(贪婪匹配)
1 个或多个字符(贪婪匹配)
0 个或多个字符(贪婪匹配)
以上三个取第一个匹配结果(非贪婪匹配)
对于前一个字符重复m到n次,{m}亦可
对于前一个字符重复m到n次,并取尽可能少
特殊字符转义或者特殊序列
表示一个字符集
或
匹配括号中任意表达式
注释,可忽略
Matches if ... matches next, but doesn't consume the string.
Matches if ... doesn't match next.
Matches if preceded by ... (must be fixed length).
Matches if not preceded by ... (must be fixed length).
说明
'^hello'匹配'helloworld'而不匹配'aaaahellobbb'
与上同理
<*>匹配
与上同理
与上同理
<*>匹配
a{6}匹配6个a、a{2,4}匹配2到4个a
‘aaaaaa’中a{2,4}只会匹配2个
[0-9]、[a-z]、[A-Z]、[^0]
A|B,或运算
'(?=test)' 在hellotest中匹配hello
'(?!=test)' 若hello后面不为test,匹配hello
'(?<=hello)test' 在hellotest中匹配test
'(?
正则表达式特殊序列表如下:
特殊序列符号
A
Z
b
B
意义
只在字符串开始进行匹配
只在字符串结尾进行匹配
匹配位于开始或结尾的空字符串
匹配不位于开始或结尾的空字符串
d
D
s
S
w
W
相当于[0-9]
相当于[^0-9]
匹配任意空白字符:[tnrrv]
匹配任意非空白字符:[^tnrrv]
匹配任意数字和字母:[a-zA-Z0-9]
匹配任意非数字和字母:[^a-zA-Z0-9]
match
(pattern, string[, flags])
match(string[, pos[, endpos]])
作用:match() 函数只在字符串的开始位置尝试匹配正则表达式,也就是只报告从位置 0 开始的匹配情况,而 search() 函数是扫描整个字
符串来查找匹配。如果想要搜索整个字符串来寻找匹配,应当用 search()。
下面是几个例子:
例:最基本的用法,通过bject对象调用
1. #!/usr/bin/env python
2. import re
3.
4. r1 = e(r'world')
5. if ('helloworld'):
6. print 'match succeeds'
7. else:
8. print 'match fails'
9.
10. if ('helloworld'):
11. print 'search succeeds'
12. else:
13. print 'search fails'
说明一下:r是raw(原始)的意思。因为在表示字符串中有一些转义符,如表示回车'n'。如果要表示表需要写为''。但如果我就是需要表
示一个''+'n',不用r方式要写为:'n'。但使用r方式则为r'n'这样清晰多了。
例:设置flag
1. #r2 = e(r'n$', re.S)
2. #r2 = e('n$', re.S)
3. r2 = e('World$', re.I)
4. if ('helloworldn'):
5. print 'search succeeds'
6. else:
7. print 'search fails'
8.
例:直接调用
1. if (r'abc','helloaaabcdworldn'):
2. print 'search succeeds'3. else:
4. print 'search fails'
split
(pattern, string[, maxsplit=0, flags=0])
split(string[, maxsplit=0])
作用:可以将字符串匹配正则表达式的部分割开并返回一个列表
例:简单分析ip
1. #!/usr/bin/env python
2. import re
3. r1 = e('W+')
4. print ('192.168.1.1')
5. print ('(W+)','192.168.1.1')
6. print ('(W+)','192.168.1.1',
1)
结果如下:
['192', '168', '1', '1']
['192', '.', '168', '.', '1', '.', '1']
['192', '.', '168.1.1']
findall
l(pattern, string[, flags])
findall(string[, pos[, endpos]])
作用:在字符串中找到正则表达式所匹配的所有子串,并组成一个列表返回
例:查找[]包括的内容(贪婪和非贪婪查找)
1. #!/usr/bin/env python
2. import re
3.
4. r1 = e('([.*])')
5. print l(r1,"hello[hi]heldfsdsf[iwonder]lo")
6. r1 = e('([.*?])')
7. print l(r1,"hello[hi]heldfsdsf[iwonder]lo")
8.
9. print l('[0-9]{2}',"fdskfj1323jfkdj")
10. print l('([0-9][a-z])',"fdskfj1323jfkdj")
11. print l('(?=www)',"afdsfwwwfkdjfsdfsdwww")
12. print l('(?<=www)',"afdsfwwwfkdjfsdfsdwww")
finditer
er(pattern, string[, flags])
finditer(string[, pos[, endpos]])
说明:和 findall 类似,在字符串中找到正则表达式所匹配的所有子串,并组成一个迭代器返回。同样 RegexObject 有:
sub
(pattern, repl, string[, count, flags])
sub(repl, string[, count=0])
说明:在字符串 string 中找到匹配正则表达式 pattern 的所有子串,用另一个字符串 repl 进行替换。如果没有找到匹配 pattern 的串,
则返回未被修改的 string。Repl 既可以是字符串也可以是一个函数。
例:1. #!/usr/bin/env python2. import re3.
4. p = e('(one|two|three)')5. print ('num','one word two words three wordsapple', 2)(pattern, repl, string[, count, flags])subn(repl, string[, count=0])说明:该函数的功能和 sub() 相同,但它还返回新的字符串以及替换的次数。同样 RegexObject 有:


发布评论