我需要匹配以下字符串 File system full。问题是开始 F 可以是小写或大写。当字符串比较通常区分大小写时,我如何在 Python 中执行此操作?
请您参考如下方法:
为了简洁起见,我将提供 bool 指示符供您使用(而不是实际的 if block 。
使用正则表达式:
import re
bool(re.match('[F|f]',<your string>)) #if it matched, then it's true. Else, false.
如果字符串可以在你的输出中的任何地方(我假设是字符串)
import re
bool(re.search('[F|f]ile system full',<your string>))
其他选项:
检查 'f' 和 'F'
<your string>[0] in ('f','F')
<your string>.startswith('f') or <your string>.startswith('F')
还有之前建议的 lower 方法:
<your string>.lower() == 'f'
