假设你希望在字符串中查找电话号码。你知道模式:3 个数字,一个短横线,3
个数字,一个短横线,再是 4 个数字。例如:415-555-4242。
假定我们用一个名为 isPhoneNumber()的函数,来检查字符串是否匹配模式,它返回 True 或 False。打开一个新的文件编辑器窗口,输入以下代码,然后保存为
isPhoneNumber.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'))
运行该程序,输出看起来像这样:
415-555-4242 is a phone number:
True
Moshi moshi is a phone number:
False
isPhoneNumber()函数的代码进行几项检查,看看 text 中的字符串是不是有效的电话号码。如果其中任意一项检查失败,函数就返回 False。代码首先检查该字符串是否刚好有 12
个字符➊。然后它检查区号(就是 text 中的前 3
个字符)是否只包含数字➋。函数剩下的部分检查该字符串是否符合电话号码的模式:号码必须在区号后出现第一个短横线➌,3
个数字➍,然后是另一个短横线➎,最后是 4
个数字➏。如果程序执行通过了所有的检查,它就返回True➐。
用参数'415-555-4242'调用 isPhoneNumber()将返回真。用参数'Moshi moshi'调用 isPhoneNumber()将返回假,第一项测试失败了,因为不是 12
个字符。
必须添加更多代码,才能在更长的字符串中寻找这种文本模式。用下面的代码,替代 isPhoneNumber.py 中最后 4 个 print()函数调用:
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')
该程序运行时,输出看起来像这样:
Phone number found: 415-555-1011 Phone number found: 415-555-9999 Done
在 for 循环的每一次迭代中,取自 message 的一段新的 12 个字符被赋给变量 chunk➊。例如,在第一次迭代,i 是 0,chunk 被赋值为
message[0:12](即字符串'Call me at 4')。在下一次迭代,i 是1,chunk 被赋值为message[1:13](字符串'all me at 41')。将 chunk
传递给 isPhoneNumber(),看看它是否符合电话号码的模式➋。如果符
合,就打印出这段文本。
继续遍历 message,最终 chunk 中的 12 个字符会是一个电话号码。该循环遍历了整个字符串,测试了每一段 12 个字符,打印出所有满足
isPhoneNumber()的chunk。当我们遍历完 message,就打印出Done。
在这个例子中,虽然 message
中的字符串很短,但它也可能包含上百万个字符,程序运行仍然不需要一秒钟。使用正则表达式查找电话号码的类似程序,运行也不会超过一秒钟,但用正则表达式编写这类程序会快得多。