Python核心编程正则表达式练习题1-1 识别后续的字符串:“bat”、“bit”、“but”、“hat”、“hit”或者“hut”

# 1-1 识别后续的字符串:“bat”、“bit”、“but”、“hat”、“hit”或者“hut”。
 import re

 # 正则表达式,|元字符表示选择“或”
# character = 'bat|bit|but|hat|hit|hut' # 方法一
# character = '[bh][aiu]t' # 方法二
character = '(b|h)(a|i|u)t' # 方法三 # 测试数据
data = 'bat'
data1 = 'bit'
data2 = 'but'
data3 = 'hat'
data4 = 'hit'
data5 = 'hut' # 测试开始
m = re.match(character, data)
m1 = re.match(character, data1)
m2 = re.match(character, data2)
m3 = re.match(character, data3)
m4 = re.match(character, data4)
m5 = re.match(character, data5) if m is not None:
print("data与character匹配成功,结果为:")
print(m.group()) if m1 is not None:
print("data1与character匹配成功,结果为:")
print(m1.group()) if m2 is not None:
print("data2与character匹配成功,结果为:")
print(m2.group()) if m3 is not None:
print("data3与character匹配成功,结果为:")
print(m3.group()) if m4 is not None:
print("data4与character匹配成功,结果为:")
print(m4.group()) if m5 is not None:
print("data5与character匹配成功,结果为:")
print(m5.group())
"""
/usr/bin/python3.5 /studydisk/other/正则表达式/demo1.py
data与character匹配成功,结果为:
bat
data1与character匹配成功,结果为:
bit
data2与character匹配成功,结果为:
but
data3与character匹配成功,结果为:
hat
data4与character匹配成功,结果为:
hit
data5与character匹配成功,结果为:
hut &&
/usr/bin/python3.5 /studydisk/other/正则表达式/demo1.py
data与character匹配成功,结果为:
bat
data1与character匹配成功,结果为:
bit
data2与character匹配成功,结果为:
but
data3与character匹配成功,结果为:
hat
data4与character匹配成功,结果为:
hit
data5与character匹配成功,结果为:
hut &&
data与character匹配成功,结果为:
bat
data1与character匹配成功,结果为:
bit
data2与character匹配成功,结果为:
but
data3与character匹配成功,结果为:
hat
data4与character匹配成功,结果为:
hit
data5与character匹配成功,结果为:
hut """
上一篇:.NET的HTTP辅助类:RestSharp


下一篇:android:theme决定AlertDialog的背景颜色