【问题标题】:Remove everything between the first opening parenthesis & and it's respective closing parenthesis (parenthesis included). Is this possible?删除第一个左括号 & 及其各自的右括号(包括括号)之间的所有内容。这可能吗?
【发布时间】:2021-10-22 23:17:00
【问题描述】:

我有一个包含很多行文本的文件,如下所示:
(文字)

发生这种情况的情况很多:
在 textfile1.txt 中:
logging.info(文本文本文本(文本文本)文本文本)

如果我使用:
$ sed -i '/(/,/)/d' textfile
退货 '文本文本)'

还有很多这样的例子:
(文字
文字文字(文字文字)
文字文字)

我想删除第一个左括号 & 和它各自的右括号(包括两个括号)之间的所有内容。不管用什么方法,随便找。这可能吗?

我尝试编写自己的 python 脚本,但我什至没有远程接近。寻找不同的解决方案比修复我认为的损坏的程序更容易。

我看过其他一些帖子,例如:
Find text between Opening parenthesis closing [closed]
match opening parenthesis to the corresponding closing parenthesis
regex: parse opening closing parenthesis with other parenthesis in between 但是我对正则表达式真的很不好,他们都使用正则表达式,我不知道

【问题讨论】:

    标签: python sed scripting grep


    【解决方案1】:

    只需使用 awk,例如给定这个输入:

    $ cat file
    (text)
    logging.info(text text text(text text) text text)
    foo (text
    text text(text text)
    text text) bar
    

    然后将 GNU awk 用于多字符 RS:

    $ awk -v RS='^$' -v ORS= '{ while( gsub(/\([^()]*)/,"") ); } 1' file
    
    logging.info
    foo  bar
    

    否则在每个 Unix 机器上的任何 shell 中使用任何 awk:

    $ cat tst.awk
    { rec = rec $0 ORS }
    END {
        while ( gsub(/\([^()]*)/,"",rec) );
        printf "%s", rec
    }
    

    $ awk -f tst.awk file
    
    logging.info
    foo  bar
    

    【讨论】:

    • 您好,这是一个很好的解决方案,但我发现了一个缺陷。在我的测试中,如果“没有匹配括号的括号”在一组匹配的括号内,它会中断;如:在textfile.txt中:text("text ( text text")或text(text text The character for a right parenthesis looks like ")". text text text)
    • 编辑您的问题以提供包含该案例的示例输入。编写脚本时我们所要做的就是您告诉我们脚本必须能够处理。
    【解决方案2】:

    您可以让 GNU sed 循环,直到所有对都被删除:

    sed -z ':a;s/([^()]*)//;ta' textfile1.txt
    

    编辑:按照 Ed 的建议,我添加了 -z。

    【讨论】:

      【解决方案3】:

      使用 sed:

      sed ':a
      s/([^()]*)//g;t a
      /(/!b
      $b
      N;b a' file
      

      删除平衡的(...),直到没有剩余。如果( 仍然存在并且尚未读取最后一行输入:将下一行添加到模式空间,然后重复。

      【讨论】:

        【解决方案4】:
        def a(test_str):
          ret = ''
          skip1c = 0
          skip2c = 0
          for i in test_str:
              if i == '[':
                  skip1c += 1
              elif i == '(':
                  skip2c += 1
              elif i == ']' and skip1c > 0:
                  skip1c -= 1
              elif i == ')'and skip2c > 0:
                  skip2c -= 1
              elif skip1c == 0 and skip2c == 0:
                  ret += i
          return ret
        
        x = "ewq[a [(b] ([c))]] This is a sentence. (once a day) [twice a day]"
        x = a(x)
        print x
        print repr(x)
        

        【讨论】:

        • 这将有助于将最终结果添加到您的答案中并选择更具描述性的变量名称。此外,除非您指定这是针对 Python 2,否则请使用带括号的 print()。
        【解决方案5】:

        您可以使用诸如find_parens() 发布here 之类的函数来查找字符串中匹配括号的索引。如果字符串的括号不平衡,此函数也会引发异常。

        对于一个字符串

        s = "This is a (little (really small)) test (?)." 
        

        调用find_parens(s) 给出:

        {18: 31, 10: 32, 39: 41}
        

        索引被组织成一个字典。我们可以将其替换为按元组的第一个元素排序的元组列表:

        sorted(list(find_parens(s).items()), key=lambda x: x[0])
        

        这给出了:

        [(10, 32), (18, 31), (39, 41)]
        

        第一个元组给出字符串中第一个左括号的索引及其匹配的右括号:

        bounds = sorted(list(find_parens(s).items()), key= lambda x: x[0])[0]
        print(bounds)
        

        这给出了:

        (10, 32)
        

        剩下的就是删除由这些索引定义的字符串部分:

        out = s[:bounds[0]] + s[bounds[1]+1:]
        print(out)
        

        我们得到:

        'This is a  test (?).'
        

        【讨论】:

          猜你喜欢
          • 2016-10-16
          • 1970-01-01
          • 1970-01-01
          • 2017-04-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-17
          相关资源
          最近更新 更多