1

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

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

代码:

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,否则系统将找不到搜索的单词。

4

3 回答 3

0
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']
于 2018-05-02T10:02:41.217 回答
0

re.findall将参数作为re.findall(pattern, string, flags=0).

import re
s = 'the existing word is Charles'
print(re.findall(r'charles', s, re.IGNORECASE))
# ['Charles']

re.IGNORECASE确保不区分大小写的匹配。

于 2018-05-02T10:03:18.487 回答
0

问题在于 thematch=re.sub(x,RepX,TextString)它还需要参数标志。所以它变成了thematch=re.sub(x,RepX,TextString,flags= re.M|re.I)

于 2018-05-02T20:58:19.687 回答