python正则表达式匹配指定的字符开头和指定的字符结束

时间:2021-09-02 06:01:09   收藏:0   阅读:14

一,使用python的re.findall函数,匹配指定的字符开头和指定的字符结束

代码示例:

1 import re
2 # re.findall函数;匹配指定的字符串开头和指定的字符串结尾(前后不包含指定的字符串)
3 str01 = ‘hello word‘
4 str02 = re.findall(‘(?<=e).*?(?=r)‘,str01)
5 print(str02)

输出结果:

1 [‘llo wo‘]

二,使用python的re.findall函数,匹配指定的字符开头和指定的字符结束(前后包含指定的字符串)

注意:

代码示例:

1 import re
2 # re.findall函数;匹配指定的字符串开头和指定的字符串结尾(前后包含指定的字符串)
3 str01 = ‘hello word‘
4 str02 = re.findall(‘h.*o‘,str01)
5 print(str02)

输出结果:

1 [‘hello wo‘]
1 import re
2 # re.findall函数; .*? 如果匹配的字符中有多个相同的匹配结尾值的
3 str01 = ‘hello word‘
4 str02 = re.findall(‘h.*?o‘,str01)
5 print(str02)

输出结果:

1 [‘hello‘]

 

 

 

import re
# re.findall函数;匹配指定的字符串开头和指定的字符串结尾(前后包含指定的字符串)
str01 = ‘hello word‘
str02 = re.findall(‘h.*o‘,str01)
print(str02)

原文:https://www.cnblogs.com/YouJeffrey/p/15209895.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!