import re
doc_content = 'i love Audi i love audi i love AuDi and audis but not audits or audiences'
results = re.findall(r'\baudi[s]?\b', doc_content, re.IGNORECASE) #The ? metacharacter will match only one 's' following audi to include the plural form and the \b at the end will exclude other words that begin with audi.
print(results)
['Audi', 'audi', 'AuDi', 'audis']
import re
doc_content = 'i love Audi i love audi i love AuDi... but not audis'
results = re.findall(r'\baudi\b', doc_content, re.IGNORECASE) #use \b at start and end to match whole word. This will exclude audis.
print(results)
['Audi', 'audi', 'AuDi']
尝试正则表达式,在其中查找word并忽略大小写
下面是Python中正则表达式的链接-https://docs.python.org/3/howto/regex.html
一个非常简单的解决方案是使用正则表达式
这对我有用。我只是在找这个\b解决了我的问题。谢谢:)
相关问题 更多 >
编程相关推荐