如何匹配字符串中的子字符串,忽略大小写

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

我试着说:

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

但无视案件不成功。我需要在给定的文本文件中找到一组单词。我正在逐行读取文件。行上的单词可以是 MandyMandyMANDY等等(我不想使用 toupper/tolower等等)。

我正在寻找与下面的 Perl 代码等价的 Python。

if ($line=~/^Mandy Pande:/i)
206153 次浏览

There's another post here. Try looking at this.

BTW, you're looking for the .lower() method:

string1 = "hi"
string2 = "HI"
if string1.lower() == string2.lower():
print "Equals!"
else:
print "Different!"

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
import re
if re.search('(?i)Mandy Pande:', line):
...

See this.

In [14]: re.match("mandy", "MaNdY", re.IGNORECASE)
Out[14]: <_sre.SRE_Match object at 0x23a08b8>
a = "MandY"
alow = a.lower()
if "mandy" in alow:
print "true"

work around

Try:

if haystackstr.lower().find(needlestr.lower()) != -1:
# True

you can also use: s.lower() in str.lower()

You can use in operator in conjunction with lower method of strings.

if "mandy" in line.lower():

If it is a pandas series, you can mention case=False in the str.contains

data['Column_name'].str.contains('abcd', case=False)

OR if it is just two string comparisons try the other method below

You can use casefold() method. The casefold() method ignores cases when comparing.

firstString = "Hi EVERYONE"
secondString = "Hi everyone"


if firstString.casefold() == secondString.casefold():
print('The strings are equal.')
else:
print('The strings are not equal.')

Output:

The strings are equal.

One can use the in operator after applying str.casefold to both strings.

str.casefold is the recommended method for use in case-insensitive comparison.

Return a casefolded copy of the string. Casefolded strings may be used for caseless matching.

Casefolding is similar to lowercasing but more aggressive because it is intended to remove all case distinctions in a string. For example, the German lowercase letter 'ß' is equivalent to "ss". Since it is already lowercase, lower() would do nothing to 'ß'; casefold() converts it to "ss".

The casefolding algorithm is described in section 3.13 of the Unicode Standard.

New in version 3.3.

For case-insensitive substring search:

needle = "TEST"
haystack = "testing"
if needle.casefold() in haystack.casefold():
print('Found needle in haystack')

For case-insensitive string comparison:

a = "test"
b = "TEST"
if a.casefold() == b.casefold():
print('a and b are equal, ignoring case')