如何在不区分大小写的情况下匹配字符串中的子字符串

问题描述:

我正在寻找Python中忽略大小写字符串的比较.

I'm looking for ignore case string comparison in Python.

我尝试过:

if line.find('mandy') >= 0:

但是忽略大小写没有成功.我需要在给定的文本文件中找到一组单词.我正在逐行读取文件.一行上的单词可以是 mandy Mandy MANDY 等.(我不想使用toupper/等).

but no success for ignore case. I need to find a set of words in a given text file. I am reading the file line by line. The word on a line can be mandy, Mandy, MANDY, etc. (I don't want to use toupper/tolower, etc.).

我正在寻找下面与Perl代码等效的Python.

I'm looking for the Python equivalent of the Perl code below.

if ($line=~/^Mandy Pande:/i)

如果您不想使用str.lower(),则可以使用正则表达式:

If you don't want to use str.lower(), you can use a regular expression:

import re

if re.search('mandy', 'Mandy Pande', re.IGNORECASE):
    # Is True