Skip to main content
 首页 » 编程设计

python之正则表达式之检索分隔符之间的文本

2025年05月04日45sharpest

我有以下字符串:

1) there is a problem 2) There appears to be a bug 3) stuck on start screen.  

我想获取 1)、2) 和 3) 之后的文本。这就是我要找的:

['there is a problem', 'There appears to be a bug', 'stuck on start screen'] 

我尝试使用 re.split 并在 \d+ 上拆分,但这并没有给我想要的结果。我想保持解决方案的通用性,所以如果有 4) 或 5) 或更多,我仍然可以检索文本。

任何帮助将不胜感激。

请您参考如下方法:

您可以使用以下正则表达式 ( see regex demo ):

\d+\)\s* 

它匹配:

  • \d+ - 一个或多个数字
  • \) - 文字 )
  • \s* - 零个或多个空白符号。

参见 the code demo

import re 
s = "1) there is a problem 2) There appears to be a bug 3) stuck on start screen."; 
print ([x for x in re.split(r"\d+\)\s*", s) if x]); 
# => ['there is a problem ', 'There appears to be a bug ', 'stuck on start screen.']