简体   繁体   English

如何在python中使用regex -re.findall查找大写和smallCase

[英]how to find uppercase and smallCase using regex -re.findall in python

I have a code that search for expressions and highlight the matching word . 我有一个搜索表达式并突出显示匹配单词的代码。

I need to find the match regardless if its upper or lower cases i need the search to ignore the case sensitive . 我需要找到匹配项,而不管它的大小写是什么,我都需要搜索以忽略大小写。

code: 码:

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)

example the searched word : charles 查词示例: 查尔斯

the existing word is Charles 现有的单词是查尔斯

the system will not find the searched word unless i wrote Charles . 除非我写了Charles,否则系统将找不到搜索到的单词。

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']

re.findall takes parameters as re.findall(pattern, string, flags=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 ensures a case-insensitive match. re.IGNORECASE确保不区分大小写的匹配。

The problem was in thematch=re.sub(x,RepX,TextString) it need also parameter flags. 问题出在thematch=re.sub(x,RepX,TextString)它还需要参数标志。 so it becomes thematch=re.sub(x,RepX,TextString,flags= re.M|re.I) 因此它变成thematch=re.sub(x,RepX,TextString,flags= re.M|re.I)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM