目录

1. 使用正则表达式将电话号码从一段文字抽离出来:

2. 正则表达式使用re.search()时,使用group方法,将各个分组内容输出:

3. 以正则表达式概念,搜寻字符串:

4. 正则表达式的应用,分离所需格式的短句:

5. 在正则表达式中,说明通配符 * 的功能:

6. 在正则表达式中,请说明sub()和subn()的用法,同时用程序实例解说:

7. 将下列字符串转化成下列列表:


1. 使用正则表达式将电话号码从一段文字抽离出来:

import re 

msg1 = 'Please call my secretary using 0932-432-521 or 1555-562-245'
msg2 = '请明天早上8:00和我一起去打球'
msg3 = '请明天早上8:00和我一起去打球,可用2342-523-516联系我'

def parseString(string):
    '''解析字符串是否含有电话号码'''
    phoneRule = re.compile(r'(\d{4}-\d{3}-\d{3})')
    phonenum = phoneRule.findall(string)
    print('电话号码是:' + str(phonenum))

parseString(msg1)#电话号码是:['0932-432-521', '1555-562-245']
parseString(msg2)#电话号码是:[]
parseString(msg3)#电话号码是:['2342-523-516']

2. 正则表达式使用re.search()时,使用group方法,将各个分组内容输出:

group(num):匹配的整个表达式的字符串,group() 可以一次输入多个组号,在这种情况下它将返回一个包含那些组所对应值的元组;

groups():返回一个包含所有小组字符串的元组,从 1 到 所含的小组号。

import re
msg = 'Please call my secretary using 02-833-16878 '
pattern = r'(\d{2})-(\d{3})-(\d{5})'
phonenum = re.search(pattern, msg)

print('完整号码是:' + phonenum.group())
print('完整号码是:' + phonenum.group(0))
print('区域号码是:%s,区域号码是:%s%s'%(phonenum.groups()))
print('区域号码是:' + phonenum.group(1))
print('区域号码是:{}'.format(phonenum.group(2, 3)))

3. 以正则表达式概念,搜寻字符串:

  • ?符号表示可有可无
  • *符号表示0到多次
  • +符号表示1到多次
import re
msg = 'Kud, Kuder and Kudson will attend my party tonight.'
pattern1 = 'Kud(er|son)'
txts1 = re.findall(pattern1, msg)
print(txts1)
for txt in txts1:
    print('Kud'+txt)
print('\n')

pattern2 = 'Kud(er|son)?' # ?符号表示可有可无
txts2 = re.findall(pattern2, msg)
print(txts2)
for txt in txts2:
    print('Kud'+txt)
print('\n')

msg2 =  'coraler will attend my party tonight.'
pattern3 = 'coral((kud)?er)' #?符号表示可有可无,0到1次
pattern4 = 'coral((kud)*er)' #*符号表示0到多次
txts3 = re.search(pattern3, msg2)
print(txts3.group())
txts4 = re.search(pattern4, msg2)
print(txts4.group())
print('\n')

msg3 = 'coralkuder will attend my party tonight.'
pattern5 = 'coral((kud)?er)' #?符号表示可有可无,0到1次
pattern6 = 'coral((kud)*er)' #*符号表示0到多次
pattern7 = 'coral((kud)+er)' #+符号表示1到多次
txts5 = re.search(pattern5, msg3)
print(txts5.group())
txts6 = re.search(pattern6, msg3)
print(txts6.group())
txts7 = re.search(pattern7, msg3)
print(txts7.group())
print('\n')

msg4 = 'coralkudkudkuder will attend my party tonight.'
pattern8 = 'coral((kud)*er)' #*符号表示0到多次
pattern9 = 'coral((kud)+er)' #+符号表示1到多死
txts8 = re.search(pattern8, msg4)
print(txts8.group())
txts9 = re.search(pattern9, msg4)
print(txts9.group())

4. 正则表达式的应用,分离所需格式的短句:

  • \d+:表示不限长度的数字;
  • \s  :表示空格;
  • \w+:表示不限长度的数字、字母和下划线连续字符;
import re
msg = 'There are 1 pig, 2 cats, 3 dogs in my yard.'
pattern = '\d+\s\w+'
txts = re.findall(pattern, msg)
for txt in txts:
    print(txt)

5. 在正则表达式中,说明通配符 * 的功能:

. 与 *组合,可以搜寻所有字符;

import re
msg = 'Name: kuuud-welcome you;Address:8a hrb-hlj-China.com'
pattern = 'Name:(.*);Address:(.*)'
txt = re.search(pattern, msg)
Name, Address = txt.groups()
print('Name:%s,Address:%s'%(Name, Address))

6. 在正则表达式中,请说明sub()和subn()的用法,同时用程序实例解说:

 sub (pattern,newstr,msg):返回的是字符串;

subn(pattern,newstr,msg):返回的是元祖;

msg = 'Kud lake will attend my party tonight. My best friend is Kud lake.'
pattern = 'Kud lake'
newstr = 'Bebie coral'
txt1 = re.sub(pattern, newstr, msg)
txt2 = re.subn(pattern, newstr, msg)
print('取代成功:', txt1)
print('取代成功:%s, 取代次数:%d'%(txt2[0],txt2[1]))

7. 将下列字符串转化成下列列表:

split(pattern, string):pattern表示匹配的正则表达式;string表示要匹配的字符串;
string = 'city:haerbin,number 50'
result = re.split(r':|,| ',string)
print(result)

算法技巧专栏

https://blog.csdn.net/weixin_53919192/category_11761989.htmlicon-default.png?t=M3K6https://blog.csdn.net/weixin_53919192/category_11761989.html