简体   繁体   English

在 python 中使用正则表达式查找确切的字符串

[英]find exact string using regular expression in python

I am trying to work with a fuzzy control language file, the file structure looks like this:我正在尝试使用模糊控制语言文件,文件结构如下所示:

FUNCTION_BLOCK tipper   
VAR_INPUT               
    service : REAL;
    food : REAL;
END_VAR

VAR_OUTPUT              
    tip : REAL;
END_VAR

FUZZIFY service         
    TERM poor := (0, 1) (4, 0) ; 
    TERM good := (1, 0) (4,1) (6,1) (9,0);
    TERM excellent := (6, 0) (9, 1);
END_FUZZIFY

FUZZIFY food            
    TERM rancid := (0, 1) (1, 1) (3,0) ;
    TERM delicious := (7,0) (9,1);
END_FUZZIFY

DEFUZZIFY tip           
    TERM cheap := (0,0) (5,1) (10,0);
    TERM average := (10,0) (15,1) (20,0);
    TERM generous := (20,0) (25,1) (30,0);
    METHOD : COG;       
    DEFAULT := 0;       
END_DEFUZZIFY


RULEBLOCK
    AND : MIN;  
    ACT : MIN;  
    ACCU : MAX; 

    RULE 1 : IF service IS poor OR food IS rancid THEN tip IS cheap;
    RULE 2 : IF service IS good THEN tip IS average; 
    RULE 3 : IF service IS excellent AND food IS delicious THEN tip IS generous;
END_RULEBLOCK

END_FUNCTION_BLOCK

So, I am trying to extract information from FUZZIFY blocks, which start with FUZZIFY variableName , and end with END_FUZZIFY .所以,我试图从 FUZZIFY 块中提取信息,这些块以FUZZIFY variableName开头,以END_FUZZIFY结尾。 I used我用了

def get_fuzzify_terms():
    with open('fcl.txt') as infile:
        copy = False
        for line in infile:
            if "FUZZIFY" in line.strip():
                copy = True
                if "END_DEFUZZIFY" in line.strip():
                    copy =False
                    continue
                if "DEFUZZIFY" in line.strip():
                    copy = False
                    continue
                print(line)
            elif "END_FUZZIFY" in line.strip():
                copy = False
                continue
            elif copy:
                if re.findall("TERM",line):
                    print(line)

but this code is too lengthy to me, because there are too many checkpoints, also since DEFUZZIFY and END_DEFUZZIFY contain word FUZZIFY as well, so DEFUZZIFY and END_DEFIZZIFY lines are also included, which is not desired.但是这段代码对我来说太长了,因为检查点太多,而且因为 DEFUZZIFY 和 END_DEFUZZIFY 也包含单词 FUZZIFY,所以还包括了 DEFUZZIFY 和 END_DEFIZZIFY 行,这是不希望的。 Is there an easier way to find only FUZZIFY lines, and exclude those DEFUZZIFY and END_DEFUZZIFY lines有没有更简单的方法来只查找 FUZZIFY 行,并排除那些 DEFUZZIFY 和 END_DEFUZZIFY 行

import re
matches = re.findall(r"\bFUZZIFY.*?END_FUZZIFY\b", your_text, re.DOTALL);

\b: word boundary to your word start with FUZZIFY and not DEFUZZIFY \b:单词边界以 FUZZIFY 而不是 DEFUZZIFY 开头

re.DOTALL: to allow '.' re.DOTALL:允许 '.' special character to match in end of line character '\n' as well在行尾字符 '\n' 中匹配的特殊字符

Checkout the example:查看示例:

https://regex101.com/r/Lah3gu/3 https://regex101.com/r/Lah3gu/3

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

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