python核心编程第15章练习

1.识别下列字符串:"bit,""bat," "but," "hat," "hit," 或 "hut,"
"'
from re import match
def raw_input():
word = raw_input("input:")
m = match("[bh][aiu]t", word)
if m is not None:
print(m.group())

"'

2.匹配用一个空格分隔的任意一对单词,比如,名和姓
"'
import re
def raw_input():
name = raw_input("input:")
m =re.match(".+\s.+", name)
if m is not None:
print (m.group())
'"
.匹配任何字符(换行符除外)
+匹配前面出现的正则表达式一次或多次

pattern = r'\b\w+ \w+\b'
\b匹配单词边界
\w匹配任何数字字母字符,和[A-Za-z0-9]相同

你可能感兴趣的:(python核心编程第15章练习)