【问题标题】:regex to match paragraph in between 2 substrings正则表达式匹配两个子字符串之间的段落
【发布时间】:2021-10-29 06:49:15
【问题描述】:

我的字符串如下所示:

string=""
( 2021-07-10 01:24:55 PM GMT )TEST  
---  
Badminton is a racquet sport played using racquets to hit a shuttlecock across
a net. Although it may be played with larger teams, the most common forms of
the game are "singles" (with one player per side) and "doubles" (with two
players per side).  
  
  

  

( 2021-07-10 01:27:55 PM GMT )PATRICKWARR  
---  
Good morning, I am doing well. And you?  
  
  

  
  
  
---  
  
  
  
  
---  
  
* * *""

我正在尝试将字符串拆分为:

text=['羽毛球是一种球拍运动,使用球拍击打 毽子过网。虽然它可能与更大的团队一起玩, 最常见的游戏形式是“单人”(每人一个玩家 边)和“双打”(每边有两名球员)。','早上好,我是 做得好。你呢?']

我尝试过的:

text=re.findall(r'\( \d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2} PM GMT \)\w+  [\S\n]---  .*',string)

我不知道如何提取多行。

【问题讨论】:

    标签: python-3.x regex string


    【解决方案1】:

    你可以使用

    (?m)^\(\s*\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\s*[AP]M\s+GMT\s*\)\w+\s*\n---\s*\n(.*(?:\n(?!(?:\(\s*\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\s*[AP]M\s+GMT\s*\)\w+\s*\n)?---).*)*)
    

    请参阅regex demo详情

    • ^ - 行首
    • {left_rx} - 左边界
    • --- - 三个连字符
    • \s*\n - 零个或多个空格,然后是一个 LF 字符
    • (.*(?:\n(?!(?:{left_rx})?---).*)*) - 第 1 组:
      • .* - 尽可能多的零个或多个除换行符以外的字符
      • (?:\n(?!(?:{left_rx})?---).*)* - 零个或多个(甚至是空的,由于.*)行不以(可选)左边界模式开头,后跟---

    left_rx中定义的边界模式是\(\s*\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\s*[AP]M\s+GMT\s*\)\w+\s*\n,和原来基本一样,我用\s*匹配任意零个或多个空格或\s+匹配一个或多个“单词”之间的空格”。

    Python demo

    import re
    text = '''string=""\n( 2021-07-10 01:24:55 PM GMT )TEST  \n---  \nBadminton is a racquet sport played using racquets to hit a shuttlecock across\na net. Although it may be played with larger teams, the most common forms of\nthe game are "singles" (with one player per side) and "doubles" (with two\nplayers per side).  \n  \n  \n\n  \n\n( 2021-07-10 01:27:55 PM GMT )PATRICKWARR  \n---  \nGood morning, I am doing well. And you?  \n  \n  \n\n  \n  \n  \n---  \n  \n  \n  \n  \n---  \n  \n* * *""'''
    left_rx = r"\(\s*\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\s*[AP]M\s+GMT\s*\)\w+\s*\n"
    rx = re.compile(fr"^{left_rx}---\s*\n(.*(?:\n(?!(?:{left_rx})?---).*)*)", re.M)
    print ( [x.strip().replace('\n', ' ') for x in rx.findall(text)] )
    

    输出:

    ['Badminton is a racquet sport played using racquets to hit a shuttlecock across a net. Although it may be played with larger teams, the most common forms of the game are "singles" (with one player per side) and "doubles" (with two players per side).', 'Good morning, I am doing well. And you?']
    

    【讨论】:

      【解决方案2】:

      其中一种方法:

      import re
      # Replace all \n with ''
      string = string.replace('\n', '')
      
      # Replace the date string '( 2021-07-10 01:27:55 PM GMT )PATRICKWARR ' and string like '* * *' with ''
      string = re.sub(r"\(\s*\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2} [AP]M GMT\s*\)\w+|\*+", '', string)
      
      data = string.split('---')
      data = [item.strip() for item in data if item.strip()]
      print (data)
      

      输出:

      ['Badminton is a racquet sport played using racquets to hit a shuttlecock acrossa net. Although it may be played with larger teams, the most common forms ofthe game are "singles" (with one player per side) and "doubles" (with twoplayers per side).', 'Good morning, I am doing well. And you?']
      

      【讨论】:

        猜你喜欢
        • 2012-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多