正则表达式删除散列注释排除大括号

Regex remove hash comments exclude curly brackets

我想删除 # 之后的注释和字符串,但有一种特殊情况,如果 # 在大括号内(仅当 # 出现在大括号之前),则不被视为注释。

输入:

    This is 1. # Comment 1.
  This is 2. # Comment 2

  #Comment 3 {#Commit4}
  This is 3.

  # Commit5

  This is 4.{#This is 5} # Commit6

    #Commit7

  This is 6.

  # Commit8 #Commit9
  #Commit10
  # Commit11
  #Commit12 #Commit13
  {  # This is 7}; { # This is 8 } # Commit14
  {# This is 9}; { # This is 10 }={# This is 11} {# This is 12}={# This is 13}# Commit15
  
  # Commit16 {# Commit17   }

输出:

This is 1.
This is 2.
This is 3.
This is 4.{#This is 5} 
This is 6.
{  # This is 7}; { # This is 8 }
{# This is 9}; { # This is 10 }={# This is 11} {# This is 12}={# This is 13}

我想实现python3内置re模块提供的sub函数并提供我的示例代码,但是我无法删除所有#(根据我的定义)

reStr = str(input)
reStr = re.sub(r"((^|\n)(.*[^{])?)(#[^\n]+)", r'', reStr)
print (f"{reStr}")

如果你有更好的解决方案请告诉我, 谢谢

您可以使用正则表达式模式 (#[^{}\n]+)($|{) 并替换为 。 见 https://regex101.com/r/grDMqH/1

利用不允许重叠匹配的事实,这是一种方法:

({.*?})|#.*

替换为:

查看在线演示 here