1 2 Fork me on GitHub 6

正则中re.I、 re.S、re.M的作用(Python)

import re
text='''Apple-1
orange-2'''


result1=re.findall(r'apple',text)
print('result1:',result1) # result1: []
# re.I   大小写不敏感
result2 = re.findall(r'apple', text,re.I)
print('result2:', result2) # result2: ['Apple']


result3 = re.findall(r'Apple.*?orange', text)
print('result3:', result3) # result3: []
# re.S   使.匹配包括换行在内的所有字符
result4 = re.findall(r'Apple.*?orange', text, re.S)
print('result4:', result4) # result4: ['Apple-1\n    orange']


result5 = re.findall(r'\d$', text)
print('result5:', result5) # result5: ['2']
# re.M   多行匹配
result6 = re.findall(r'\d$', text, re.M)
print('result6:', result6) # result6: ['1', '2']

posted @ 2022-04-14 14:27  peng_li  阅读(391)  评论(0编辑  收藏  举报
1