如何匹配字符串中的子字符串,忽略大小写 [英] How to match a substring in a string, ignoring case
本文介绍了如何匹配字符串中的子字符串,忽略大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在 Python 中寻找忽略大小写字符串比较.
I'm looking for ignore case string comparison in Python.
我尝试过:
if line.find('mandy') >= 0:
但忽略大小写没有成功.我需要在给定的文本文件中找到一组单词.我正在逐行读取文件.一行上的单词可以是mandy、Mandy、MANDY等(我不想用touppercode>/
tolower
等).
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
这篇关于如何匹配字符串中的子字符串,忽略大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文