V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
woshichuanqilz
V2EX  ›  问与答

请教一个正则表达式的问题, 如何匹配多行且行数不确定的内容?

  •  
  •   woshichuanqilz · 2020-07-15 00:15:28 +08:00 · 1471 次点击
    这是一个创建于 1353 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我现在有文本是这样的

    ###
    1.
    2.
    xxxxx
    ###
    1.
    2. 
    1.
    2.
    1.
    xxxx
    1. <---- 这行不会被匹配因为不连续
    

    首先我需要匹配三个井号 然后再连续行中匹配以 1. 或者 2. 开头的行, 整个匹配需要是行数连续的, 以 1. 或者 2. 开头的行数不一定。

    请问一下这个有没有办法写成正则表达式?我没有想出来怎么写, 我的方法是便利每一行,然后再去做处理。但是感觉如果有正则表达式能直接匹配出来的话,会很方便

    11 条回复    2020-07-15 10:47:50 +08:00
    dbw9580
        1
    dbw9580  
       2020-07-15 00:53:56 +08:00 via Android
    ^###\n((?:\d+\..*\n)+)
    这样可以匹配,但是捕获到的是一连串好几行。如果改成内层捕获,捕获组只会捕获最后一次匹配的内容。如果想获得数字行后面的文本,可以考虑用词法分析的方法( lexer )。
    imn1
        2
    imn1  
       2020-07-15 02:04:20 +08:00
    多个正则很方便,一个也可以
    其实把###视为起始,xxxxx 视为结束就行,你上面写的“不连续”基本上就确定 xxxx 是结束标志了
    woshichuanqilz
        3
    woshichuanqilz  
    OP
       2020-07-15 06:30:30 +08:00 via Android
    @imn1 xxx 不一定是结束标志 我用 xxx 指代其他文本
    deplives
        4
    deplives  
       2020-07-15 07:39:47 +08:00
    分行处理,只匹配每行以 1. 开头的行
    imdong
        5
    imdong  
       2020-07-15 07:49:28 +08:00 via iPhone
    /^[\#]{3,}\s([0-9]+\.\s+){2,}/

    没有管是否 xxxx 结尾。

    手机盲写,未测试,可能不好使。
    woshichuanqilz
        6
    woshichuanqilz  
    OP
       2020-07-15 09:36:34 +08:00
    @dbw9580

    ```
    import re
    text = """
    LTOPOALG: ### got the S1 jump, sline 36, ts 1594566036, mbox mt-000000001004
    LTOPOALG: mt-000000001012 [0 0]: 2485 2485 [ 2485 2485 2485 2485 ] 2485 2485
    LTOPOALG: mt-000000001016 [0 0]: 50758 50752 [ 50746 50740 50734 50728 ] 50722 50716
    LTOPOALG: mt-000000001010 [0 0]: 3477 3477 [ 3477 3477 3477 3477 ] 3477 3477
    LTOPOALG: mt-000000001011 [0 0]: 55265 55263 [ 55261 55259 55257 55255 ] 55253 55251
    LTOPOALG: mt-000000001014 [0 0]: 555 555 [ 555 555 555 555 ] 555 555
    LTOPOALG: mt-000000001013 [0 0]: 793 792 [ 791 790 789 788 ] 787 786
    LTOPOALG: mt-000000001015 [0 0]: 2053 2053 [ 2053 2053 2053 2053 ] 2053 2053
    LTOPOALG: mt-000000001009 [0 0]: 348 354 [ 360 366 372 378 ] 384 390
    LTOPOALG: mt-000000001007 [0 0]: 99 99 [ 99 99 99 99 ] 99 99
    LTOPOALG: mt-000000001002 [0 0]: 3442 3442 [ 3442 3442 3442 3442 ] 3442 3442
    LTOPOALG: mt-000000001006 [0 0]: 152 152 [ 152 152 152 152 ] 152 152
    LTOPOALG: mt-000000001018 [0 0]: 1382 1396 [ 1410 1424 1438 1452 ] 1466 1480
    LTOPOALG: mt-000000001017 [0 0]: 6699 6681 [ 6663 6645 6627 6609 ] 6591 6573
    LTOPOALG: mt-000000001004 [0 1]: 25831 25852 [ 25852 21595 5897 4851 ] 4847 4849
    LTOPOALG: mt-000000001008 [0 0]: 89 89 [ 89 89 89 89 ] 89 89
    LTOPOALG: bt-000000001001-1 [0 0]: 0 0 [ 0 0 0 0 ] 0 0
    LTOPOALG: bt-000000001005-1 [0 0]: 0 0 [ 0 0 0 0 ] 0 0
    LTOPOALG: bt-000000001003-1 [0 0]: 0 0 [ 0 0 0 0 ] 0 0
    LTOPOALG: bt-000000001020-1 [0 0]: 0 0 [ 0 0 0 0 ] 0 0
    LTOPOALG: bt-000000001001-2 [0 0]: 3522 3522 [ 3522 3522 3522 3522 ] 3522 3522
    LTOPOALG: bt-000000001005-2 [0 0]: 34861 34865 [ 34889 34845 18830 13368 ] 13376 13395
    LTOPOALG: bt-000000001003-2 [0 0]: 302 306 [ 310 314 318 322 ] 326 330
    LTOPOALG: bt-000000001020-2 [0 0]: 119 119 [ 119 119 119 119 ] 119 119
    LTOPOALG: bt-000000001001-3 [0 0]: 55414 55412 [ 55410 55408 55406 55404 ] 55402 55400
    LTOPOALG: bt-000000001020-3 [0 0]: 7339 7337 [ 7335 7333 7331 7329 ] 7327 7325
    LTOPOALG: bt-000000001003-3 [0 0]: 2357 2357 [ 2357 2357 2357 2357 ] 2357 2357
    LTOPOALG: bt-000000001005-3 [0 0]: 3684 3685 [ 3686 3687 3688 3689 ] 3690 3691
    LTOPOALG: bt-000000001020-4 [0 0]: 956 958 [ 960 962 964 966 ] 968 970
    LTOPOALG: bt-000000001001-4 [0 0]: 50406 50405 [ 50404 50403 50402 50401 ] 50400 50399
    LTOPOALG: bt-000000001003-4 [0 0]: 403 403 [ 403 403 403 403 ] 403 403
    LTOPOALG: bt-000000001005-4 [0 0]: 7499 7505 [ 7511 7517 7523 7529 ] 7535 7541
    LTOPOALG: bt-000000001003-5 [0 0]: 817 816 [ 815 814 813 812 ] 811 810
    LTOPOALG: bt-000000001020-5 [0 0]: 25925 25949 [ 25947 25955 6115 4841 ] 4835 4847
    LTOPOALG: bt-000000001001-5 [0 0]: 2304 2304 [ 2304 2304 2304 2304 ] 2304 2304
    LTOPOALG: bt-000000001005-5 [0 0]: 113987 -1 [ -1 -1 113612 113682 ] 113752 113822
    LTOPOALG: **ltopo_alg_proc_s1_jump, got the SINGLE jump, sline 36, box 000000001004, father 000000000522
    LTOPOALG: gen ev mt-000000001004
    """

    regex = "^.*###.*\n(.*(mt).*\n)+"
    for i in re.findall(regex, text, re.M):
    print('-' * 20)
    print(i)
    ```

    问下我这么写的代码, 为什么匹配到的结果。
    是这样
    > ('LTOPOALG: mt-000000001008 [0 0]: 89 89 [ 89 89 89 89 ] 89 89\n', 'mt')
    imn1
        7
    imn1  
       2020-07-15 09:38:20 +08:00
    @woshichuanqilz #3
    你怎么转不过弯来呢?
    既然是其他,那就肯定不是 1/2 开头了吧,那 \n[^12] 自然就是结束标志了
    woshichuanqilz
        8
    woshichuanqilz  
    OP
       2020-07-15 09:50:22 +08:00
    @imdong
    text = """
    ###
    1.
    2.
    xxxxx
    ###
    1.
    2.
    1.
    2.
    1.
    xxxx
    1. <---- 这行不会被匹配因为不连续
    """

    regex = "^[\#]{3,}\s([0-9]+\.\s+){2,}"
    for i in re.findall(regex, text, re.M):
    print('-' * 20)
    print(i)

    这个是我的测试结果:
    --------------------
    2.

    --------------------
    1.
    woshichuanqilz
        9
    woshichuanqilz  
    OP
       2020-07-15 10:17:06 +08:00
    @imn1

    谢谢 按照你的思路弄出来了


    [code]
    import re
    text = """
    LTOPOALG: ### got the S1 jump, sline 36, ts 1594566036, mbox mt-000000001004
    LTOPOALG: mt-000000001012 [0 0]: 2485 2485 [ 2485 2485 2485 2485 ] 2485 2485
    LTOPOALG: mt-000000001016 [0 0]: 50758 50752 [ 50746 50740 50734 50728 ] 50722 50716
    LTOPOALG: mt-000000001010 [0 0]: 3477 3477 [ 3477 3477 3477 3477 ] 3477 3477
    LTOPOALG: mt-000000001011 [0 0]: 55265 55263 [ 55261 55259 55257 55255 ] 55253 55251
    LTOPOALG: mt-000000001014 [0 0]: 555 555 [ 555 555 555 555 ] 555 555
    LTOPOALG: mt-000000001013 [0 0]: 793 792 [ 791 790 789 788 ] 787 786
    LTOPOALG: mt-000000001015 [0 0]: 2053 2053 [ 2053 2053 2053 2053 ] 2053 2053
    LTOPOALG: mt-000000001009 [0 0]: 348 354 [ 360 366 372 378 ] 384 390
    LTOPOALG: mt-000000001007 [0 0]: 99 99 [ 99 99 99 99 ] 99 99
    LTOPOALG: mt-000000001002 [0 0]: 3442 3442 [ 3442 3442 3442 3442 ] 3442 3442
    LTOPOALG: mt-000000001006 [0 0]: 152 152 [ 152 152 152 152 ] 152 152
    LTOPOALG: mt-000000001018 [0 0]: 1382 1396 [ 1410 1424 1438 1452 ] 1466 1480
    LTOPOALG: mt-000000001017 [0 0]: 6699 6681 [ 6663 6645 6627 6609 ] 6591 6573
    LTOPOALG: mt-000000001004 [0 1]: 25831 25852 [ 25852 21595 5897 4851 ] 4847 4849
    LTOPOALG: mt-000000001008 [0 0]: 89 89 [ 89 89 89 89 ] 89 89
    LTOPOALG: bt-000000001001-1 [0 0]: 0 0 [ 0 0 0 0 ] 0 0
    LTOPOALG: bt-000000001005-1 [0 0]: 0 0 [ 0 0 0 0 ] 0 0
    LTOPOALG: bt-000000001003-1 [0 0]: 0 0 [ 0 0 0 0 ] 0 0
    LTOPOALG: bt-000000001020-1 [0 0]: 0 0 [ 0 0 0 0 ] 0 0
    LTOPOALG: bt-000000001001-2 [0 0]: 3522 3522 [ 3522 3522 3522 3522 ] 3522 3522
    LTOPOALG: bt-000000001005-2 [0 0]: 34861 34865 [ 34889 34845 18830 13368 ] 13376 13395
    LTOPOALG: bt-000000001003-2 [0 0]: 302 306 [ 310 314 318 322 ] 326 330
    LTOPOALG: bt-000000001020-2 [0 0]: 119 119 [ 119 119 119 119 ] 119 119
    LTOPOALG: bt-000000001001-3 [0 0]: 55414 55412 [ 55410 55408 55406 55404 ] 55402 55400
    LTOPOALG: bt-000000001020-3 [0 0]: 7339 7337 [ 7335 7333 7331 7329 ] 7327 7325
    LTOPOALG: bt-000000001003-3 [0 0]: 2357 2357 [ 2357 2357 2357 2357 ] 2357 2357
    LTOPOALG: bt-000000001005-3 [0 0]: 3684 3685 [ 3686 3687 3688 3689 ] 3690 3691
    LTOPOALG: bt-000000001020-4 [0 0]: 956 958 [ 960 962 964 966 ] 968 970
    LTOPOALG: bt-000000001001-4 [0 0]: 50406 50405 [ 50404 50403 50402 50401 ] 50400 50399
    LTOPOALG: bt-000000001003-4 [0 0]: 403 403 [ 403 403 403 403 ] 403 403
    LTOPOALG: bt-000000001005-4 [0 0]: 7499 7505 [ 7511 7517 7523 7529 ] 7535 7541
    LTOPOALG: bt-000000001003-5 [0 0]: 817 816 [ 815 814 813 812 ] 811 810
    LTOPOALG: bt-000000001020-5 [0 0]: 25925 25949 [ 25947 25955 6115 4841 ] 4835 4847
    LTOPOALG: bt-000000001001-5 [0 0]: 2304 2304 [ 2304 2304 2304 2304 ] 2304 2304
    LTOPOALG: bt-000000001005-5 [0 0]: 113987 -1 [ -1 -1 113612 113682 ] 113752 113822
    LTOPOALG: **ltopo_alg_proc_s1_jump, got the SINGLE jump, sline 36, box 000000001004, father 000000000522
    LTOPOALG: gen ev mt-000000001004
    """


    regex = "LTOPOALG: ### .*?\n(?!LTOPOALG: mt)(?!LTOPOALG: bt)"
    for i in re.findall(regex, text, re.M|re.DOTALL):
    print('-' * 20)
    print(i)

    [/code]
    justgodlike1993
        10
    justgodlike1993  
       2020-07-15 10:19:12 +08:00
    #{3}\r?\n([12].*\r?\n){2,}
    imn1
        11
    imn1  
       2020-07-15 10:47:50 +08:00
    #6
    1.findall 是匹配括号内的,你看看你的括号指代什么
    2.findall 要慎用*,多个*结果难以预料,应该在确定存在的地方使用+,并且配合?限制最少匹配
    3.以我的理解,python 遇到 ()+ 这种形式,它匹配多次,但只返回最后一个,match 等等都是这样

    你这个话题,建议分开多条正则
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5652 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 06:31 · PVG 14:31 · LAX 23:31 · JFK 02:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.