python正则表达式02

1.查找第一个匹配的字符串

# 在此导入python正则库
########## Begin ##########
import re

########## End ##########

check_name = input()
# 在此使用正则匹配'张明'的信息,结果存储到is_zhangming中
########## Begin ##########
is_zhangming=re.search(r"张明",check_name)

########## End ##########

if is_zhangming is not None:
    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 ##########

123python456
['1', '2', '3', '4', '5', '6']
['p', 'y', 't', 'h', 'o', 'n']

4.基础正则表达式–快捷方式

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 is not None:
    print(a.span())
else:
    print(a)


# 2、编写获取到以educoder结束的正则,并存储到变量b
########## Begin ##########
b = re.search(r'python$', input_str)

########## End ##########
if b is not None:
    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)

12-3456
['12', '34', '56']
['3456']

12345
['12', '34']
['12345']

9.开闭区间与速写

# coding=utf-8

import 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)


123-456789
['456789']
['123', '456789']12345-678
['12345']
['12345', '678']
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据攻城小狮子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值