regex-如何指定要排除的表达式

regex-如何指定要排除的表达式,regex,python-3.x,Regex,Python 3.x,我需要将两个字符{,}替换为{\n,\n}。 但是它们不能被'或'包围 我尝试了这段代码来实现这一点 text = 'hello(){imagine{myString("HELLO, {WORLD}!")}}' replaced = re.sub(r'{', "{\n", text) Ellipsis... 当然,此代码将替换引号中的花括号 什么是否定的陈述像或不是 下面是我想要的 hello(){ imagine{ puts("{HELLO}") } } 简而言之,我想做的是 搜索{和}

我需要将两个字符
{
}
替换为
{\n
\n}
。 但是它们不能被
'
'
包围

我尝试了这段代码来实现这一点

text = 'hello(){imagine{myString("HELLO, {WORLD}!")}}'
replaced = re.sub(r'{', "{\n", text)
Ellipsis...
当然,此代码将替换引号中的花括号

什么是否定的陈述像
不是

下面是我想要的

hello(){
imagine{
puts("{HELLO}")
}
}
简而言之,我想做的是

  • 搜索
    {
    }
  • 如果未包含在
    ''
    ''
  • {
    }
    替换为
    {\n
    \n}
  • 在相反的情况下,我可以用
    (?p\“*){(?p.*?”)
    来解决它。
    但是我不知道如何解决这个问题。

    首先用
    {\n
    替换所有
    {/code>字符。您还将用
    {\n
    替换
    {\code>。现在,您可以用
    {\n
    替换所有
    {\n
    字符


    您可以检查输入的相似性并尝试匹配它们

    text = 'hello(){imagine{puts("{HELLO}")}}'
    replaced = text.replace('){', '){\n').replace('{puts', '{\nputs').replace('}}', '\n}\n}')
    print(replaced)
    
    输出:

    hello(){
    imagine{
    puts("{HELLO}")
    }
    }
    
    更新


    尝试以下操作:

    {
    替换为
    {\n

    text.replace('{', '{\n')
    
    }
    替换为
    \n}

    text.replace('}', '\n}')
    
    现在修复引用的大括号:

    text.replace('"{\n','"{')
    

    结合起来:

    replaced = text.replace('{', '{\n').replace('}', '\n}').replace('"{\n','"{').replace('\n}"', '}"')
    
    输出

    hello(){
    imagine{
    puts("{HELLO}")
    }
    }
    

    您可以匹配单引号和双引号(C样式)字符串文字(那些支持带反斜杠的转义实体的文字),然后在任何其他可以替换为所需值的上下文中匹配
    {
    }

    见:

    重新导入
    text='hello(){imagine{put({hello}”)}'
    
    dblq=r'(?您必须支持什么类型的字符串文字(以避免在中匹配大括号)?它们可能包含什么类型的实体?我只想支持引号。可能仅此而已。如果您想在字符串中编写
    ”,它是
    “text\”text
    还是
    “text”“text”
    ?我想使用第一个。谢谢你的帮助。你在使用Python 3.5和更新版本吗?谢谢你的回答。但是文本可以更改。它也可以是
    想象{{dragons}}
    函数“imagine”也可以更改?是的,它可以更改。可能所有的东西都可以更改。好的,那么输入数据之间有什么相似之处呢?这样做的目的是识别代码是否与“{”,“}”断行但是要小心,因为双引号中可能有一些括号?谢谢你的回答。但是,引号和花括号并不总是连续的。如果你确定答案不起作用,请更新你的问题以进一步概述限制条件!:)很抱歉我迟到了。此解决方案将替换“H{E”以“H{\nE”。但是它不需要替换。我很感激。你的解释对我很有帮助。另外,regex101是一个非常好的站点!谢谢你的回答!
    replaced = text.replace('{', '{\n').replace('}', '\n}').replace('"{\n','"{').replace('\n}"', '}"')
    
    hello(){
    imagine{
    puts("{HELLO}")
    }
    }
    
    import re
    text = 'hello(){imagine{puts("{HELLO}")}}'
    dblq = r'(?<!\\)(?:\\{2})*"[^"\\]*(?:\\.[^"\\]*)*"'
    snlq = r"(?<!\\)(?:\\{2})*'[^'\\]*(?:\\.[^'\\]*)*'"
    rx = re.compile(r'({}|{})|[{{}}]'.format(dblq, snlq))
    print(rx.pattern)
    def repl(m):
        if m.group(1):
            return m.group(1)
        elif m.group() == '{':
            return '{\n'
        else:
            return '\n}'
    
    # Examples
    print(rx.sub(repl, text))
    print(rx.sub(repl, r'hello(){imagine{puts("Nice, Mr. \"Know-all\"")}}'))
    print(rx.sub(repl, "hello(){imagine{puts('MORE {HELLO} HERE ')}}"))
    
    ((?<!\\)(?:\\{2})*"[^"\\]*(?:\\.[^"\\]*)*"|(?<!\\)(?:\\{2})*'[^'\\]*(?:\\.[^'\\]*)*')|[{}]
    
    (?<!\\)((?:\\{2})*(?:"[^"\\]*(?:\\.[^"\\]*)*"|'[^'\\]*(?:\\.[^'\\]*)*'))|[{}]