首页 > 解决方案 > 如何在python中使用正则表达式-re.findall查找大写和小写

问题描述

我有一个搜索表达式并突出显示匹配单词的代码。

无论是大写还是小写,我都需要找到匹配项,我需要搜索以忽略区分大小写。

代码:

RepX='<u><b style="color:#FF0000">'+x+'</b></u>'


    for counter , myLine in enumerate(filename):

        #added
        self.textEdit_PDFpreview.clear()
        thematch=re.sub(x,RepX,TextString)
        thematchFilt=re.findall(x,TextString,re.M|re.IGNORECASE)

例如搜索词:charles

现有的词是查尔斯

除非我写了Charles,否则系统将找不到搜索的单词。

标签: pythonregexfindallignore-case

解决方案


import re


text = "234422424"
text2 = "My text"


print( re.findall( r'^[A-Öa-ö\s]+', text)) # []
print( re.findall( r'^[A-Öa-ö\s]+', text2)) # ['My text']

推荐阅读