简体   繁体   English

Python中的正则表达式模式可在文件内搜索文本块

[英]Regex pattern in Python to search for text block inside a file

Hello I am new to regex and I am trying to compile a regex to match the text block below, there are multiple such text blocks in a file and I would like to move these blocks to another file. 您好,我是regex的新手,我正在尝试编译一个regex以匹配下面的文本块,一个文件中有多个这样的文本块,我想将这些块移动到另一个文件中。 Kindly help me! 请帮助我!

conversion DC_PRESS_1024 {
    kind = conversionKind rationalFunction;
    physMin = 0;
    physMax = 10;
    coefficients = ( 
        0;
        1024;
        0;
        0;
        0;
        1;
    )
    comments = 
        commentVal {
            value = 
                mlString {
                    value = "[VIT_DDS]External_CompuMethod";
                    language = language en;
                }
            kind = commentKind internal;
        }
}

Note: The contents inside conversion XX_XXXX_XXX{ } vary. 注意:转换XX_XXXX_XXX {}中的内容有所不同。 I want to search for the entire block irrespective of the name ie XX_XXXX_XXX and the content inside {}. 我想搜索整个块,而不管名称是XX_XXXX_XXX还是{}中的内容。

The difficult bit here is to manage opening and closing curly brackets. 这里的难点是管理大括号的打开和关闭。 If you have potentially unlimited depth for nesting the brackets, then I don't think you can do it with regular expressions, because it would be a recursive pattern. 如果嵌套嵌套的深度可能不受限制,那么我认为您不能使用正则表达式来实现,因为这将是递归模式。 In that case you would need a parser , keeping track of the opened brackets and reading the text token by token. 在这种情况下,您将需要一个解析器 ,跟踪打开的括号并逐个令牌读取文本令牌。

But if you know already that it is impossible to have more than two levels of nesting inside blocks (like in your example), then you can use a regular expression like this one: 但是,如果您已经知道不可能在块内嵌套两个以上级别的嵌套(例如在您的示例中),那么可以使用如下正则表达式:

conversion\s*\w+\s*\{([\s\w\;\=\(\)\[\]\"]|(\{([\s\w\;\=\(\)\[\]\"]|(\{[\s\w\;\=\(\)\[\]\"]+\}))+\}))+\}

See it in action at: http://regexr.com/3b794 请通过以下网址查看实际运行情况: http : //regexr.com/3b794

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

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