使用正则表达式在文本中搜索特定的单词列表。但它给出的结果不正确

问题描述 投票:-3回答:1
import re  
complex_sen_count = 0  
sen = '5th grade. Very easy to read. Easily understood by an average 11-year-old student.'  
search_list = [',', 'after', 'although', 'as', 'because', 
               'before', 'even though', 'if', 'since', 'though', 
               'unless', 'until', 'when', 'whenever', 'whereas', 
               'wherever','while']  
s = sen.split('. ')  
for n in s:   
    print(n)  
    if re.compile('|'.join(search_list),re.IGNORECASE).search(n):   
        complex_sen_count+=1   
print("value: ",complex_sen_count)   

该值应返回0,因为字符串中没有“search_list”字样。但它仍在增加变量complex_sen_count。 输出是: 5th grade Very easy to read Easily understood by an average 11-year-old student. value: 2

预期产量:0 请帮忙。

python regex
1个回答
1
投票

正好有2场比赛:

'五年级。很容易阅读。一名11岁的普通学生容易理解。

要搜索单词,请在单词之前和之后添加空格,例如:\sas\s\s表示空格)。

© www.soinside.com 2019 - 2024. All rights reserved.