【问题标题】:Regex to append some characters in a certain position正则表达式在某个位置附加一些字符
【发布时间】:2020-02-29 21:08:45
【问题描述】:

我有一个 txt 文件,如下所示:

abandon(icl>leave>do,agt>person,obj>person);CAT(CATV),AUX(AVOIR),VAL1(GN) ; 

我想用正则表达式修改它,因为它是一个很长的 txt。 我想在每个 CAT(...) 之前和第一个“;”之后附加每行的第一个单词。应该还有第二个“;”在附加的单词之后和 CAT 之前。我该怎么做?

所以我的输出将是:

abandon(icl>leave>do,agt>person,obj>person);abandon;CAT(CATV),AUX(AVOIR),VAL1(GN) ;

【问题讨论】:

    标签: python regex


    【解决方案1】:

    您可以在正则表达式模式下尝试以下查找和替换:

    Find:    ^([^(]+)(.*?;)(CAT.*)$
    Replace: $1$2$1;$3
    

    这里的想法是将每一行细分为我们需要将替换线程连接在一起的部分。在这种情况下,第一个捕获组是我们计划在第一个分号之后、CAT 之前插入的单词。

    Demo

    刚刚注意到您使用的是 Python。我们可以试试:

    inp = """aarhus(iof>city>thing,equ>arhus);CAT(CATN),N(NP) ;
    abadan(iof>city>thing);CAT(CATN),N(NP) ;
    abandon(icl>leave>do,agt>person,obj>person);CAT(CATV),AUX(AVOIR),VAL1(GN) ;"""
    output = re.sub(r'([^(]+)(.*?;)(CAT.*?;)\s*', '\\1\\2\\1;\\3\n', inp)
    print(output)
    

    打印出来:

    aarhus(iof>city>thing,equ>arhus);aarhus;CAT(CATN),N(NP) ;
    abadan(iof>city>thing);abadan;CAT(CATN),N(NP) ;
    abandon(icl>leave>do,agt>person,obj>person);abandon;CAT(CATV),AUX(AVOIR),VAL1(GN) ;
    

    【讨论】:

    • 这是用 Python 写的吗?
    • @GDJS 对不起,我错过了你的 Python 标签。我添加了一个似乎可行的简短脚本。
    【解决方案2】:

    在 Python 中,您可以这样做:

    import re
    
    test_strings = [
        'aarhus(iof>city>thing,equ>arhus);CAT(CATN),N(NP) ;',
        'abadan(iof>city>thing);CAT(CATN),N(NP) ;',
        'abandon(icl>leave>do,agt>person,obj>person);CAT(CATV),AUX(AVOIR),VAL1(GN) ;' 
    ]
    # first group matches the wordthat you want to repeat, then you capture the rest
    # until the ;CAT which you capture separately
    regex = r'(\w+)(.*)(;CAT.*)'
    
    new_strings = []
    for test_string in test_strings:
        match = re.match(regex, test_string)
        new_string = match.group(1) + match.group(2) + ";" + match.group(1) + match.group(3)
        new_strings.append(new_string)
        print(new_string)
    

    给你:

    aarhus(iof>city>thing,equ>arhus);aarhus;CAT(CATN),N(NP) ;
    abadan(iof>city>thing);abadan;CAT(CATN),N(NP) ;
    abandon(icl>leave>do,agt>person,obj>person);abandon;CAT(CATV),AUX(AVOIR),VAL1(GN) ;
    

    您的字符串存储在new_strings 列表中。

    编辑: 要将文件读取为准备修改的字符串列表,只需使用with open 语句并执行readlines():

    my_file = 'my_text_file.txt'
    
    with open(my_file, 'r') as f:
        my_file_as_list = f.readlines()
    

    【讨论】:

    • 我无法将我的文本作为唯一字符串导入。这是一个巨大的 txt 文件(对于我只提取前三行的问题)。
    • @GDJS 那是一个不同的问题,您可以搜索如何将文件读入字符串列表。一般来说,这很简单,我会针对一般情况更新我的答案。
    【解决方案3】:

    匹配不同的组和编织可能比正则表达式替换更快。必须测试

    import re
    
    #=== DESIRED ===================================================================
    # aarhus(iof>city>thing,equ>arhus);aarhus;CAT(CATN),N(NP) ;
    # abadan(iof>city>thing);abadan;CAT(CATN),N(NP) ;
    # abandon(icl>leave>do,agt>person,obj>person);abandon;CAT(CATV),AUX(AVOIR),VAL1(GN) ;```
    #===============================================================================
    
    data = ["abadan(iof>city>thing);CAT(CATN),N(NP) ;", 
    "abandon(icl>leave>do,agt>person,obj>person);CAT(CATV),AUX(AVOIR),VAL1(GN) ;"]
    
    # Matching different groups, and then stiching together may be faster tna a regex replace. 
    # Basedon https://stackoverflow.com/questions/3850074/regex-until-but-not-including
    # (?:(?!CAT).)* - match anything until the start of the word CAT.
    # I.e.
    # (?:        # Match the following but do not capture it:
    # (?!CAT)  # (first assert that it's not possible to match "CAT" here
    #  .         # then match any character
    # )*         # end of group, zero or more repetitions.
    p = ''.join(["^", # Match start of string
                 "(.*?(?:(?!\().)*)", # Match group one, anything up to first open paren, which will be the first word (I.e. abadan or abandon
                 "(.*?(?:(?!CAT).)*)", # Group 2, match everything after group one, up to "CAT" but not including CAT
                 "(.*$)" # Match the rest
                 ])
    
    for line in data:
        m = re.match(p, line)    
        newline  = m.group(1) # First word
        newline += m.group(2) # Group two
        newline += m.group(1) + ";" # First word again with semi-colon
        newline += m.group(3) # Group three
    
        print(newline)
    

    输出:

    abadan(iof>city>thing);abadan;CAT(CATN),N(NP) ;
    abandon(icl>leave>do,agt>person,obj>person);abandon;CAT(CATV),AUX(AVOIR),VAL1(GN) ;
    

    【讨论】:

      【解决方案4】:

      此脚本读取输入文件,进行替换并写入输出文件:

      import re
      
      infile = 'input.txt'
      outfile = 'outfile.txt'
      f = open(infile, 'r')
      o = open(outfile, 'w')
      for line in f:
          o.write(re.sub(r'((\w+).+?)(?=;CAT)', r'\1;\2', line))
      

      cat outfile.txt 
      aarhus(iof>city>thing,equ>arhus);aarhus;CAT(CATN),N(NP) ;
      abadan(iof>city>thing);abadan;CAT(CATN),N(NP) ;
      abandon(icl>leave>do,agt>person,obj>person);abandon;CAT(CATV),AUX(AVOIR),VAL1(GN) ; 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-13
        • 1970-01-01
        • 1970-01-01
        • 2011-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多