目录
5、写一个正则表达式,使其能同时识别下面所有的字符串:'bat', 'bit', 'but', 'hat', 'hit', 'hut‘
1、匹配一行文字中的所有开头的字母内容
>>> import re
>>>
>>> s = "abc123 90years $hello world!"
>>> p = re.compile(r"^[a-zA-Z]+",re.M)
>>> print(p.match(s).group())
abc
2、匹配一行文字中的所有开头的数字内容
>>> s = "2022 abc123 90years $hello world!"
>>> p = re.compile(r"^\d+",re.M)
>>> print(p.match(s).group())
2022
3、匹配一行文字中的所有开头的数字内容或字母内容
>>> s = "abc123 90years $hello world!"
>>> p = re.compile(r"^\w+",re.M)
>>> print(p.match(s).group())
abc123
4、 只匹配包含字母和数字的行
import re
s = """abc123
90ww dd123
hello world!
Hi girl~
###test"""
p = re.compile(r"^\w+$",re.M)
print(p.findall(s))
5、写一个正则表达式,使其能同时识别下面所有的字符串:'bat', 'bit', 'but', 'hat', 'hit', 'hut‘
>>> s=['bat', 'bit', 'but', 'hat', 'hit', &