正则_句点统配换行

2018-08-21  本文已影响0人  测试媛617
import re
'''
传入re.DOTALL作为re.compile()的第二个参数,让句点字符匹配所有字符,包括换行
'''

noNewlineRegex = re.compile('.*')
a = noNewlineRegex.search('Serve the public trust.\nProtect the innocent.\nUpload the law.').group()
print(a)

print('-----------------')

newlineRegex = re.compile('.*',re.DOTALL)
b = newlineRegex.search('Serve the public trust.\nProtect the innocent.\nUpload the law.').group()
print(b)
结果是:
Serve the public trust.
-----------------
Serve the public trust.
Protect the innocent.
Upload the law.
上一篇 下一篇

猜你喜欢

热点阅读