xh使用正则表达式查找 headerr 的实例,然后使用一些规范编辑下面的行

问题描述 投票:0回答:1

所以我有以下 .msg 文件的摘录。我想要做的是对于所有 [sel xxx xxx] 标题找到它们,然后阅读它们下面的行。如果任何答案包含 (+3) 或任何 (+x),则将其从消息中删除。之后我想比较每个 [sel xxx xxx] 块,对于具有最高 (+x) 的块,我想在该行的初始标签之后但在文本之前添加另一个标签 [clr 4]。如果有两个答案相等 (+x),那么我希望将 [clr 4] 添加到这两行。

[msg MSG_015_0_0 [Makoto]]
[s][bup 0 6 0 65535 1][f 4 10 65535 0 0][vp 8 0 0 0 65535 0]Dummy[n][f 1 3 65535][w][e]

[sel SEL_016_0_0 top]
[s]A(+3)[e]
[s]B[e]
[s]C(+3)[e]


[msg MSG_021_0_0 [Makoto]]
[s][bup 0 6 0 65535 1][f 4 10 65535 0 0][vp 8 0 0 0 65535 0]Sorry I had to ask something so[n]unreasonable... I appreciate you[n]going along with it, though.[n][f 1 3 65535][w][e]

[sel SEL_023_0_0 top]
[s]Fist! Of! Justice![e]
[s]Report him to the police.[e]
[s][f 0 8 2 1 2217]Try to reach out to her.(+3)[clr 27][e]

所以输出应该是这样的:

[msg MSG_015_0_0 [Makoto]]
[s][bup 0 6 0 65535 1][f 4 10 65535 0 0][vp 8 0 0 0 65535 0]Dummy[n][f 1 3 65535][w][e]

[sel SEL_016_0_0 top]
[s][clr 4]A[e]
[s]B[e]
[s][clr 4]C[e]


[msg MSG_021_0_0 [Makoto]]
[s][bup 0 6 0 65535 1][f 4 10 65535 0 0][vp 8 0 0 0 65535 0]Sorry I had to ask something so[n]unreasonable... I appreciate you[n]going along with it, though.[n][f 1 3 65535][w][e]

[sel SEL_023_0_0 top]
[s]Fist! Of! Justice![e]
[s]Report him to the police.[e]
[s][f 0 8 2 1 2217][clr 4]Try to reach out to her.[clr 27][e]

我今天一直在尝试编写不同的代码片段,但我对编码非常缺乏经验,而且我永远无法真正让它正常运行并获得所需的结果。我尝试通过以下方法有条不紊地解决它:

  1. 使用正则表达式识别 [sel 标头。
  2. 找到 (+x) 的任何实例并删除它们
  3. 在具有最大值 (+x) 的行的文本之前添加 [clr 4]。
  4. 将其写回输出文件,而不进行任何格式更改(即换行符保持不变,标题保持不变,并且大部分未更改)。

这是我的尝试:

import re


def process_block(block):
    # Find all instances of `(+x)` and their positions in the block
    plus_x_matches = re.findall(r'\(\+(\d+)\)', block)
    if plus_x_matches:
        # Convert the found matches to integers and find the maximum
        max_x = max(map(int, plus_x_matches))
        # Replace all `(+x)` with an empty string
        block = re.sub(r'\(\+\d+\)', '', block)
        # Find the position where the maximum `(+x)` was found
        max_x_position = block.rfind(f'(+{max_x})')
        # If `(+x)` was found, insert `[col 4]` before the text
        if max_x_position != -1:
            # Insert `[col 4]` at the position of the max `(+x)`
            block = block[:max_x_position] + '[col 4]' + block[max_x_position:]
    return block


def main():
    # Read input file
    with open('input.msg', 'r') as infile:
        content = infile.read()

    # Split the content into blocks
    blocks = re.split(r'(\[sel .+? top\])', content)

    # Process each block
    result = []
    in_block = False
    current_block = ''
    for part in blocks:
        if part.startswith('[sel'):
            # If there's a previous block to process, do so
            if in_block:
                result.append(process_block(current_block))
                current_block = ''
            # Start a new block
            result.append(part)
            in_block = True
        elif in_block:
            # Append to the current block content
            current_block += part

    # Don't forget to process the last block
    if in_block:
        result.append(process_block(current_block))

    # Write output file
    with open('output.msg', 'w') as outfile:
        outfile.write(''.join(result))


if __name__ == '__main__':
    main()
python regex game-development python-re
1个回答
0
投票

我获取了代码库并添加了以新顺序复制和替换的逻辑。

使用多个捕获组构建了一个新模式,以获取每场比赛的内容并将其重新排列成具有所请求的替换的新文本。

此代码可以在终端或jupyter笔记本中运行。

import re
content ="""
[msg MSG_015_0_0 [Makoto]]
[s][bup 0 6 0 65535 1][f 4 10 65535 0 0][vp 8 0 0 0 65535 0]Dummy[n][f 1 3 65535][w][e]

[sel SEL_016_0_0 top]
[s]A(+3)[e]
[s]B[e]
[s]C(+3)[e]


[msg MSG_021_0_0 [Makoto]]
[s][bup 0 6 0 65535 1][f 4 10 65535 0 0][vp 8 0 0 0 65535 0]Sorry I had to ask something so[n]unreasonable... I appreciate you[n]going along with it, though.[n][f 1 3 65535][w][e]

[sel SEL_023_0_0 top]
[s]Fist! Of! Justice![e]
[s]Report him to the police.[e]
[s][f 0 8 2 1 2217]Try to reach out to her.(+3)[clr 27][e]
"""

def process_block(block):
    # Find all instances of `(+x)` and their positions in the block
    plus_x_matches = re.findall(r'\(\+(\d+)\)', block)
    print('block')
    print(block[:25])
    print('plus_x_matches')
    print(plus_x_matches)
    if plus_x_matches:
        # Convert the found matches to integers and find the maximum
        max_x = max(map(int, plus_x_matches))
        patter_find_replace = r'\[s\](\[.*?\])?(.*)(\(\+'+str(max_x)+r'\))(\[.*?\])?\[e\]'
        new_lines = []
        lines = block.split('\n')
        for l in lines :
            pattern = re.compile(patter_list_content)
            matches = pattern.findall(l)
            if(len(matches)>0):
                item = matches[0]
                replaced = '[s]'+item[0]+'[col 4]'+item[1]+item[3]+'[e]'
                new_lines.append(replaced)
            else:
                new_lines.append(l)
        block = "\n".join(new_lines)
    return block

def main():
    # Split the content into blocks
    blocks = re.split(r'(\[sel .+? top\])', content)

    # Process each block
    result = []
    in_block = False
    current_block = ''
    for part in blocks:
        if part.startswith('[sel'):
            # If there's a previous block to process, do so
            if in_block:
                result.append(process_block(current_block))
                current_block = ''
            # Start a new block
            result.append(part)
            in_block = True
        elif in_block:
            # Append to the current block content
            current_block += part

    # Don't forget to process the last block
    if in_block:
        result.append(process_block(current_block))

    print('result:')
    for r in result:
        print(r)

main()
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.