正则匹配,怎么匹配不定内容的成对的中间的内容呢? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
nyxsonsleep
V2EX    正则表达式

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

  •  
  •   nyxsonsleep 2023-10-27 20:20:23 +08:00 2332 次点击
    这是一个创建于 760 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    ... 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 条附言    2023-10-27 21:17:28 +08:00
    建议使用 python 作为示例,忘记说明了。不同语言的正则匹配库应该是略有区别的
    10 条回复    2023-10-27 22:22:19 +08:00
    Leon406
        1
    Leon406  
       2023-10-27 20:44:46 +08:00




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

    分组匹配 group1 是 js, group2 是中间内容
    Nile20
        2
    Nile20  
       2023-10-27 21:11:49 +08:00
    只保留了 xxx.js (分组的 group1 )
    ^(\w+\.js): actual (?:output|error): \{.*?^\}$

    https://imgur.com/mlj8UeY.png

    推荐一个站点 https://regex101.com/ 可以实时预览正则效果(上图),记不住的 token 有速查,不熟悉的还可以让 GPT 帮你写,然后丢进来验证
    nyxsonsleep
        3
    nyxsonsleep  
    OP
       2023-10-27 21:15:45 +08:00
    @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  
       2023-10-27 21:22:53 +08:00
    @nyxsonsleep #3
    . 匹配换行
    nyxsonsleep
        5
    nyxsonsleep  
    OP
       2023-10-27 22:03:12 +08:00
    @Leon406 匹配结果还是 0
    nyxsonsleep
        6
    nyxsonsleep  
    OP
       2023-10-27 22:04:00 +08:00
    @Nile20 需要 python 版本的。试过 GPT ,人工智障。
    Leon406
        7
    Leon406  
       2023-10-27 22:15:52 +08:00
    @nyxsonsleep #5 发下测试数据, 我测试正常匹配三条数据
    Nile20
        8
    Nile20  
       2023-10-27 22:19:58 +08:00
    @nyxsonsleep 我看你样例文本是.js 所以选的 js 。不同语言的正则库有一些差异,但是在你这个例子上并没有,只不过 python 里多行文本需要传递指定的 flags 给正则表达式

    https://imgur.com/UYk3x8B.png
    Nile20
        9
    Nile20  
       2023-10-27 22:21:31 +08:00
    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  
       2023-10-27 22:22:19 +08:00
    python 强制用缩进来表示代码级别是我一直的槽点()。这下全乱了
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1589 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 19ms UTC 16:23 PVG 00:23 LAX 08:23 JFK 11:23
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86