从字符串中查找关键字,最经常使用,也最简单的方法就是:flask
if 'a' in 'apk': print yes yes
不过有时搜索关键字时须要忽略大小写,好比在一段日志中搜索crash,日志中的crash可能会有不少种写法,好比Crash,CRASH,crash等等,那么如何作呢?日志
也很简单,首先导入re这个包code
import re
而后,好比咱们须要搜索的日志存在变量log中:字符串
log = 'CrashjdfccrashqweasdfCRASHCRashjlasdflaskdfjzxcv asdfiouqwerxzcv asdfjlqwercrAsh jlasfd' pattern = re.compile(r'crash',re.IGNORECASE) result = pattern.findall(log) result ['Crash', 'crash', 'CRASH', 'CRash', 'crAsh']
这样就能够忽略大小写找到对应的关键词了io