
10

Python正则表达式re模块使用 | CHEGVA
source link: https://chegva.com/5643.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Python高级(23)—正则表达式re模块使用
◎知识点
re模块search方法
re模块findall方法
re模块finditer方法
◎脚本练习
▽ re模块search方法
"""
当在字符串中查找指定的子串时,借助模块re并通过正则表达式指定被查找的子串可以实现更强大的查找功能。
模块re提供了三个用于实现字符串查找的方法:
1、search(pattern, string[, flags])
该方法会在参数string指定的字符串中查找参数pattern指定的第一个子串。
其中,参数pattern是一个正则表达式,或对正则表达式预编译之后得到的对象。
如果有匹配的子串,返回第一个匹配对应的Match对象;如果没有匹配的子串,返回None。
"""
import re
print(re.search(r'\d+', '-123-56-89-')) # <re.Match object; span=(1, 4), match='123'>
pattern = re.compile(r'\d+')
print(re.search(pattern, '-123-56-89-')) # <re.Match object; span=(1, 4), match='123'>
print(re.search(r'\d+', '-abc-d-efg-')) # None
"""
除了直接调用模块re的方法search()之外,也可以调用模块re的方法compile()的返回值的方法:
search(string[, pos[, endpos]])
其中,
参数pos用于指定被查找字符串的起始位置,默认值是0;
参数endpos用于指定被查找字符串的结束位置,默认值是字符串的长度。被查找的子串不包括结束位置。
模块re的方法search()中的参数pattern和flags被转移到了compile()中。
"""
print(pattern.search('-123-56-89-')) # <re.Match object; span=(1, 4), match='123'>
print(pattern.search('-123-56-89-', 4)) # <re.Match object; span=(5, 7), match='56'>
Python
▽ re模块findall方法
"""
2、findall(pattern, string[, flags])
该方法会在参数string指定的字符串中查找参数pattern指定的所有子串。
其中,参数pattern是一个正则表达式,或对正则表达式预编译之后得到的对象。
如果有匹配的子串,返回所有匹配的子串组成的列表;如果没有匹配的子串,返回空列表。
"""
print(re.findall(r'\d+', '-123-56-89-')) # ['123', '56', '89']
print(re.findall(r'\d+', '-abc-d-efg-')) # []
"""
除了直接调用模块re的方法findall()之外,也可以调用模块re的方法compile()的返回值的方法:
findall(string[, pos[, endpos]])
其中,
参数pos用于指定被查找字符串的起始位置,默认值是0;
参数endpos用于指定被查找字符串的结束位置,默认值是字符串的长度。被查找的子串不包括结束位置。
模块re的方法findall()中的参数pattern和flags被转移到了方法compile()中。
"""
print(pattern.findall('-123-56-89-')) # ['123', '56', '89']
print(pattern.findall('-123-56-89-', 4)) # ['56', '89']
Python
▽ re模块finditer方法
"""
3、finditer(pattern, string[, flags])
该方法会在参数string指定的字符串中查找参数pattern指定的所有子串。
其中,参数pattern是一个正则表达式,或对正则表达式预编译之后得到的对象。
如果有匹配的子串,返回所有匹配的子串组成的迭代器;如果没有匹配的子串,返回不包含任何元素的迭代器。
"""
iterator = re.finditer(r'\d+', '-123-56-89-')
# <re.Match object; span=(1, 4), match='123'>
# <re.Match object; span=(5, 7), match='56'>
# <re.Match object; span=(8, 10), match='89'>
for match in iterator:
print(match)
iterator = re.finditer(r'\d+', '-abc-d-efg-')
for match in iterator:
print(match)
"""
除了直接调用模块re的方法finditer()之外,也可以调用模块re的方法compile()的返回值的方法:
finditer(string[, pos[, endpos]])
其中,
参数pos用于指定被查找字符串的起始位置,默认值是0;
参数endpos用于指定被查找字符串的结束位置,默认值是字符串的长度。被查找的子串不包括结束位置。
模块re的方法finditer()中的参数pattern和flags被转移到了方法compile()中。
"""
iterator = pattern.finditer('-123-56-89-')
# <re.Match object; span=(1, 4), match='123'>
# <re.Match object; span=(5, 7), match='56'>
# <re.Match object; span=(8, 10), match='89'>
for match in iterator:
print(match)
iterator = pattern.finditer('-123-56-89-', 4)
# <re.Match object; span=(5, 7), match='56'>
# <re.Match object; span=(8, 10), match='89'>
for match in iterator:
print(match)
Python
◎脚本地址:https://github.com/anzhihe/learning/blob/master/python/practise/learn-python/python_senior/regex3.py
安志合个人博客,版权所有 丨 如未注明,均为原创 丨 转载请注明转自:https://chegva.com/5643.html | ☆★★每天进步一点点,加油!★★☆ |
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK