regex Python正则表达式通过AND条件查找匹配单词

brvekthn  于 2023-04-13  发布在  Python
关注(0)|答案(1)|浏览(142)
def count_conditions(input_str, listbox):
        electric_attributes = ['electric', 'batter', "power"]
        input_str = input_str.strip().replace('\r\n', '\n').replace('\r', '\n')
        pattern = re.compile(r'(\{AND.*?\})', re.DOTALL)
        matches = re.findall(pattern, input_str)
        
        line_num = 1

        for match in matches:
            attrs_found = []
            words = re.findall(r'\b\w+\b', match)
            for word in words:
                for attr in electric_attributes:
                    if attr in word.lower() and word not in attrs_found:
                        attrs_found.append(word)
                        
                        
            if attrs_found:
                message = "{} electrical attribute(s) found on lines {}-{} of AND condition: {}".format(
                    len(attrs_found), 
                    line_num, 
                    line_num + len(match.splitlines()) - 1, 
                    ', '.join(attrs_found)
                )
                
            else:
                message = "No electrical attribute(s) found on lines {}-{} of AND condition".format( 
                    line_num, 
                    line_num + len(match.splitlines()) - 1, 
                )
            listbox.insert("end", message)
            line_num += match.count('\n') + 1

我希望代码能够确定外部的“and”语法,而不是那些已经在AND语法内部的语法。
例如,第1-14行应该被视为1 AND语法,而不是3。

{AND
condition_1:batterie
condition_2:samsung
{OR
{AND
condition_1:TV
condition_2:LCD
}
{AND
condition_1:remote
condition_2:TV
}
}
}

{AND
condition_1:batterie
condition_2:samsung
{OR
condition_3:TV
condition_4:remote
}
}

输出应为:

请帮助!我做错了什么?或者其他方法?

jmo0nnb3

jmo0nnb31#

我根据我的评论写了一些代码(我也对你的做了一些修改,打印这些行而不是将它们插入未知的listbox对象中,并将electric_attributes更新为batterie)。

def count_conditions(input_str):
    electric_attributes = ['electric', 'batterie', "power"]
    input_lines = [line for line in input_str.split('\n') if line]
    open_brackets = 0
    for i, line in enumerate(input_lines):
        if open_brackets == 0:
            # Start of a new outer 'AND' block
            if '{AND' in line:
                open_brackets = line.count('{')
                current_block = line
                block_start = i + 1
                continue
            else: 
                # this shouldn't actually happen! But if it does, ignore the line
                continue
        current_block += line
        if '{' in line:
            open_brackets += line.count('{')
        if '}' in line:
            open_brackets -= line.count('}')
        if open_brackets == 0:   
            # We've reached the end of the current external 'AND' block
            attrs_found = [attr for attr in electric_attributes if attr in current_block]
            if attrs_found:
                l = len(attrs_found)
                print(f'{l} electrical attribute{"s" if l > 1 else ""} found on lines {block_start}-{i + 1}'
                      f' of AND condition: {" ,".join(attrs_found)}')
            else:
                print(f'No electrical attribute found on lines {block_start}-{i + 1}'
                      f' of AND condition')

示例

test='''
{AND
condition_1:batterie
condition_2:samsung
{OR
{AND
condition_1:TV
condition_2:LCD
}
{AND
condition_1:remote
condition_2:TV
}
}
}
{AND
condition_1:batterie
condition_2:samsung
{OR
condition_3:TV
condition_4:remote
}
}
'''

count_conditions(test)
# 1 electrical attribute found on lines 1-14 of AND condition: batterie
# 1 electrical attribute found on lines 15-22 of AND condition: batterie

相关问题