7.模式匹配和正则表达式
1)不用正则表达式来查找文本模式
只能采用固定的匹配模式,复杂的匹配程序过于繁琐。
测试程序:test_701.py
def isPhoneNumber(text):
if len(text) != 12:
return False
for i in range(0,3):
if not text[i].isdecimal():
return False
if text[3] != '-':
return False
for i in range(4,7):
if not text[i].isdecimal():
return False
if text[7] != '-':
return False
for i in range(8,12):
if not text[i].isdecimal():
return False
return True
print('415-555-4242 is a phone number:')
print(isPhoneNumber('415-555-4242'))
print('Moshi moshi is a phone number:')
print(isPhoneNumber('Moshi moshi'))
message = 'Call me at 415-555-1011 tomorrow . 415-555-9999 is my office.'
for i in range(len(message)):
chunk = message[i:i+12]
if isPhoneNumber(chunk):
print('Phone number found: ' + chunk)
print('Done')
2)正则表达式查找文本
创建正则表达式对象: re.compile(r’……’)
匹配Regex对象: search() group()
3)正则表达式匹配更多模式
括号分组:(***)(***)分出多组
管道匹配多个组: xxx|yyy
问号可选匹配 (xx)?
星号匹配零次或多次:(xx)*
加号匹配一次或多次:(xx)+
花括号匹配特定次数:(xx){3,5}
4)贪心或非贪心匹配 {xxx}?
最大或者最小匹配
5)findall()方法 查找所有的匹配项
search() ,查找到第一条匹配项
6)字符分类
\d \D \w \W \s \S
[0-9] [a-zA-Z]
7)自构字符分类
方括号:内部列举要匹配的字符。
前缀加‘^’,表示不匹配字符。
8)插入字符和美元字符
前缀加‘^’,表示从开始处匹配。
前缀加‘$’,表示尾部匹配。
9)通配字符
句点.称为通配字符,匹配换行符以外的所有字符。
用’.*’匹配所有字符
匹配换行符: re.DOTALL
10)正则表达式符号总结
? * + {n} {n,} {,m} {n,m} {n,m}? *? +? ^ $ .
\d \w \s \D \W \S [abc] [^abc]
11)不区分大小写的匹配
re.I re.IGNORECASE
12)sub()方法替换字符串
13)管理复杂的正则表达式
参数:re.VERBOSE
14)组合使用参数
re.IGNORECASE|re.DOTALL|re.VERBOSE
15)项目:电话号码和E-mail地址提取
测试程序:test_702.py
#!python3
# phoneAndEmail.py - Finds phone numbers and email addresses on the clipboard.
import pyperclip,re,sys
phoneRegex = re.compile(r'''(
(\d{3}|\(\d{3}\))? # area code
(\s|-|\.)? # separator
(\d{3}) # first 3 digits
(\s|-|\.) # separator
(\d{4}) # last 4 digits
(\s*(ext|x|ext.)\s*(\d{2,5}))? #extension
)''',re.VERBOSE)
# Create email regex.
emailRegex = re.compile(r'''(
[a-zA-Z0-9._%+-]+ # username
@ # @ symbol
[a-zA-Z0-9.-]+ # domain name
(\.[a-zA-Z]{2,4}) # dot-something
)''',re.VERBOSE)
# Find matches in clipboard text.
text =str(pyperclip.paste())
matches = []
for groups in phoneRegex.findall(text):
phoneNum = '-'.join([groups[1],groups[3],groups[5]])
if groups[8]!='':
phoneNum += 'x' + groups[8]
matches.append(phoneNum)
for groups in emailRegex.findall(text):
matches.append(groups[0])
# Copy results to the clipboard.
if len(matches) > 0:
pyperclip.copy('\n'.join(matches))
print('Copied to clipboard:')
print('\n'.join(matches))
else:
print('No phone numbers or email addresses found.')
test702.py的测试数据:test_702.dat
Reach Us by Email - email is the best way to reach us
Help with your order: support@nostarch.commm
Academic requests: academic@nostarch.com (Further information)
Bulk and special sales questions: sales@nostarch.com
Conference and event inquiries: conferences@nostarch.com
Errata - please send any errata reports to: errata@nostarch.com
General inquiries: info@nostarch.com
Media requests: media@nostarch.com
Proposals or editorial inquiries: editors@nostarch.com
Rights inquiries: rights@nostarch.com (Further information)
Interested in working with us?
View our current job openings
Reach Us by Mail
Mailing AddressNo Starch Press
329 Primrose Road, #42
Burlingame, CA 94010-4093
USAPrincipal Place of Business
No Starch Press, Inc.
245 8th Street
San Francisco, CA 94103
USANOTE: Below are our business phone numbers but we are a completely remote company. Please email support@nostarch.com with your questions and we will do our best to promptly resolve any issues that you may have.
Phone: 800.420.7240 or +1 415.863.9900e385
Fax: +1 415.863.9950Reach Us on Social Media
Twitter Facebook Instagram Linkedin Pinterest
8.输入验证(2024/4/16-4/17)
1)PyInputPlus模块
inputStr() inputNum() inputChoice() inputMenu() inputDateTime()
inputYesNo() inputBool() inputEmail() inputFilepath() inputPassword()
关键字参数 prompt
关键字参数 min,max,greaterThan,lessThan
关键字参数 blank
关键字参数 limit,timeout,default
关键字参数 allowRegexes,blockRegexes
自定义验证参数 inputCustom()
测试程序:test_801.py
import pyinputplus as pyip
def addsUpToTen(numbers):
numbersList = list(numbers)
for i,digit in enumerate(numbersList):
numbersList[i] = int(digit)
if sum(numbersList) != 10:
raise Exception('The digits must add up to 10,not %s.'%(sum(numbersList)))
return int(numbers) # Return an int form of numbers
response = pyip.inputCustom(addsUpToTen) #No parentheses after addsUpToTen here
2)项目:如何让人忙几小时
测试程序:test_802.py
import pyinputplus as pyip
while True:
prompt = 'Want to know how to keep a person busy for hours?\n'
response = pyip.inputYesNo(prompt)
if response == 'no':
break;
print('Thank you. Have a nice day.')
3)项目:乘法测验
测试程序:test_803.py
import pyinputplus as pyip
import random,time
numberOfQuestions = 10
correctAnswers = 0
for questionNumber in range(numberOfQuestions):
# Pick two random numbers:
num1 = random.randint(0,9)
num2 = random.randint(0,9)
prompt = '#%s:%s x %s = '%(questionNumber,num1,num2)
try:
# Right answers are handled by allowRegexes.
# Wrong answers are handled by blockRegexes, with a custom message.
pyip.inputStr(prompt,allowRegexes=['^%s$' % (num1 * num2)],
blockRegexes=[('.*','Incorrect!')],
timeout=8,limit=3)
except pyip.TimeoutException:
print('Out of time!')
except pyip.RetryLimitException:
print('Out of tries!')
else:
# This block runs if no exceptions were raised in the try block.
print('Correct!')
correctAnswers += 1
time.sleep(1) # Brief pause to let user see the result.
print('Score: %s / %s' % (correctAnswers,numberOfQuestions))