(1) 编写正则表达式,匹配所有的有效的Python标识符集合:
import re
pattern = re.compile(r"(?<![\$!`;])\b[A-Za-z_]\w*\b(?![\$!`;])")
a = "abc+acv_ M 7j !XF abc$"
print(re.findall(pattern,a))
(2) 编写正则表达式,查找下面文本中所有处于单词开头位置的字母:
Python is a great object-oriented, interpreted, and interactive programming language.
#方法一(不完全使用正则表达式):
import re
a = "Python is a great object-oriented, interpreted, and interactive programming language."
b = a.replace(',','')
c = b.replace('.','')
d = c.split(' ')
list_i = []
for i in d:
e = re.findall("^\w",i)
for w in e:
if w != '': #这里是把列表e元素重新输出再输入到新的列表里,从而避免大表套小表
list_i.append(w)
print(list_i)
方法二(基本上依靠正则表达式):
import re
a = "Python is a great object-oriented, interpreted, and interactive programming language."
b = a.replace('-','')
print(re.findall(r"\b\w",b)) #本人才疏学浅,实在是搞不定\b会将“-”前后的当做是两个单词的问题,于是只好先下手为强,用替换的方式除掉“-”
(3) 编写正则表达式,查找下面文本中所有处于单词开头位置的数字:
Pyth8on 7is a 3great 2object-oriented, inter4preted, 1an3d interactive programming 5language.
方法一(还是不完全用正则表达式):
import re
a = "Pyth8on 7is a 3great 2object-oriented, inter4preted, 1an3d interactive programming 5language."
b = a.replace(',','')
c = b.replace('.','')
d = c.split(' ')
list_i = []
for i in d:
e = re.findall("^\d",i)
for num in e:
if num != '': #因为筛选完之后位置不在词首的数字就会筛选产生空集,把空集除掉就筛选出来了
list_i.append(num)
print(list_i)
方法二(纯正的正则表达式):
import re
a = "Pyth8on 7is a 3great 2object-oriented, inter4preted, 1an3d interactive programming 5language."
print(re.findall(" (\d+)",a)) #这里的表达式也可以换成r"\b\d",效果一样,代码中那个是用单词前必存在空格的特性找出词首
(4) 编写程序,检测下列电子邮箱地址是否规范。符合规范的输出该邮箱地址,并提示其符合规范;对于不符合规范的邮箱地址,提示其不合规范,并提供`abc123@163.com`作为参考规范:
import re
pattern = '[a-z0-9]+(@)(gmail|yahoo|msn|hotmail|aol|ask|live|qq|0355|163|126|3721|yeah|googlemail|mail)\.(com|net)' #这里长长的一串邮箱就是本人试图多写几个邮箱防止测试代码出问题的见证
a = input("请输入电子邮箱:")
result = re.match(pattern,a) #比对
if result:
print(a,"合规")
else:
print("该邮箱不合规,正确的格式应当是:abc123@163.com")