# 在此导入python正则库########## Begin ##########import re
########## End ##########
check_name =input()# 在此使用正则匹配'张明'的信息,结果存储到is_zhangming中########## Begin ##########
is_zhangming=re.search(r"张明",check_name)########## End ##########if is_zhangming isnotNone:print(is_zhangming.span())else:print(is_zhangming)
张明
(0,2)0123456789 张明
(11,13)
2.基础正则表达式–字符组
import re
input_str =input()# 编写获取python和Python的正则,并存储到match_python变量中########## Begin ##########
match_python=re.findall("[pP]ython",input_str)########## End ##########print(match_python)
I like python. Python is easy.['python','Python']
3.基础正则表达式–区间与区间取反
import re
input_str =input()# 1、编写获取到数字的正则,并输出匹配到的信息########## Begin ##########
b=re.findall(r'[0-9]',input_str)print(b)########## End ########### 2、编写获取到不是数字的正则,并输出匹配到的信息########## Begin ##########
c = re.findall(r'[^0-9]',input_str)print(c)########## End ##########
import re
input_str =input()# 1、编写获取到单词的正则,并输出匹配到的信息########## Begin ##########
b=re.findall(r'[a-zA-Z0-9]',input_str)print(b)########## End ########### 2、编写获取到不是单词的正则,并输出匹配到的信息########## Begin ##########
c=re.findall(r'[\W]',input_str)print(c)########## End ##########
i love python!!!
['i','l','o','v','e','p','y','t','h','o','n'][' ',' ','!','!','!']
!@#$we['w','e']['!','@','#','$']
5.字符串的开始与结束
import re
input_str =input()# 1、编写获取到以educoder开头的正则,并存储到变量a########## Begin ##########
a = re.search(r'^python', input_str)########## End ##########if a isnotNone:print(a.span())else:print(a)# 2、编写获取到以educoder结束的正则,并存储到变量b########## Begin ##########
b = re.search(r'python$', input_str)########## End ##########if b isnotNone:print(b.span())else:print(b)
python is good
(0,6)None
end with python
None(9,15)
6.任意字符
import re
input_str =input()# 编写获取(任意字符)+ython的字符串,并存储到变量a中########## Begin ##########
a=re.findall(r'.ython',input_str)########## End ##########print(a)
dython
['dython']
7.可选字符
import re
input_str =input()# 编写获取she或者he的字符串,并存储到变量a中########## Begin ##########
a=re.findall(r's?he',input_str)########## End ##########print(a)
she
['she']
8.重复区间
import re
input_str =input()# 1、基于贪心模式匹配字符串中重复出现2个数字的子字符串,并存储到变量a。########## Begin ##########
a = re.findall(r'[\d]{2}',input_str)########## End ##########print(a)# 2、基于贪心模式匹配字符串中重复出现4-7个数字的子字符串,并存储到变量b。########## Begin ##########
b = re.findall(r'[\d]{4,7}',input_str)########## End ##########print(b)
# coding=utf-8import re
input_str =input()# 1、基于贪心模式匹配字符串中连续出现5个数字以上的子字符串,并存储到变量a。########## Begin ##########
a = re.findall(r'[\d]{5,}',input_str)########## End ##########print(a)# 2、匹配字符串中都为数字的子字符串,并存储到变量b。########## Begin ##########
b = re.findall(r'[\d]+',input_str)########## End ##########print(b)