V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
nyxsonsleep
V2EX  ›  正则表达式

正则匹配,怎么匹配不定内容的成对的中间的内容呢?

  •  
  •   nyxsonsleep · 182 天前 · 728 次点击
    这是一个创建于 182 天前的主题,其中的信息可能已经有所发展或是发生改变。

    彻底实现效果,请一杯咖啡(红包),聊表谢意。

    ...
    abc.js: actual output: {
      "loc": {
        "start": {
          "line": 1,
        },
        "end": {
          "line": 1,
        }
      }
    }
    Error: xxxx
    
    abc.js: actual error:
    ...
    

    abc.js 不是固定内容(可能有 xxx.js 等),

    abc.js 的数量不止一对,

    actual output 和 actual error 是固定存在内容的有且仅有一对。

    保留 abc.js 信息。

    需要匹配多组

    第 1 条附言  ·  182 天前
    建议使用 python 作为示例,忘记说明了。不同语言的正则匹配库应该是略有区别的
    10 条回复    2023-10-27 22:22:19 +08:00
    Leon406
        1
    Leon406  
       182 天前




    ([^.\s]+\.js): actual output: (.*?)\1: actual error

    分组匹配 group1 是 js, group2 是中间内容
    Nile20
        2
    Nile20  
       182 天前
    只保留了 xxx.js (分组的 group1 )
    ^(\w+\.js): actual (?:output|error): \{.*?^\}$

    https://imgur.com/mlj8UeY.png

    推荐一个站点 https://regex101.com/ 可以实时预览正则效果(上图),记不住的 token 有速查,不熟悉的还可以让 GPT 帮你写,然后丢进来验证
    nyxsonsleep
        3
    nyxsonsleep  
    OP
       182 天前
    @Leon406

    ```
    input_text="input_text.txt"
    output_text="output_text.txt"
    import re
    def change():
    with open(input_text) as f:
    ss=f.readlines()
    s=''.join(ss)
    pattern = re.compile(r'([^.\s]+\.js): actual output: (.*?)\1: actual error')
    rs=re.findall(pattern,s)
    with open(output_text,'w') as fw:
    for i in rs :
    print(i[0])
    fw.write(i[1])
    # print(i[1])
    print(len(rs))
    change()
    ```

    匹配结果 0 个。
    Leon406
        4
    Leon406  
       182 天前
    @nyxsonsleep #3
    . 匹配换行
    nyxsonsleep
        5
    nyxsonsleep  
    OP
       182 天前
    @Leon406 匹配结果还是 0
    nyxsonsleep
        6
    nyxsonsleep  
    OP
       182 天前
    @Nile20 需要 python 版本的。试过 GPT ,人工智障。
    Leon406
        7
    Leon406  
       182 天前
    @nyxsonsleep #5 发下测试数据, 我测试正常匹配三条数据
    Nile20
        8
    Nile20  
       182 天前
    @nyxsonsleep 我看你样例文本是.js 所以选的 js 。不同语言的正则库有一些差异,但是在你这个例子上并没有,只不过 python 里多行文本需要传递指定的 flags 给正则表达式

    https://imgur.com/UYk3x8B.png
    Nile20
        9
    Nile20  
       182 天前
    import re

    PATTERN = re.compile(r'^(\w+\.js): actual (?:output|error): \{.*?^\}$', flags=re.MULTILINE | re.DOTALL)

    txt = '''abc.js: actual output: {
    "loc": {
    "start": {
    "line": 1,
    },
    "end": {
    "line": 1,
    }
    }
    }
    Error: xxxx

    abd.js: actual error: {
    "loc": {
    "start": {
    "line": 1,
    },
    "end": {
    "line": 1,
    }
    }
    }'''

    match_ls = PATTERN.findall(txt)

    for m in match_ls:
    print(m)
    Nile20
        10
    Nile20  
       182 天前
    python 强制用缩进来表示代码级别是我一直的槽点╮(╯▽╰)╭。这下全乱了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2835 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 09:52 · PVG 17:52 · LAX 02:52 · JFK 05:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.