python - 如何确保捕获组超过 5 个字符?

标签 python regex

我正在使用这段代码:

(?i)(?<!see )(?<!\d)(?<!")(?<!“)ITEM.*?1A.*?\n*(?<!")(?<!“)RISK.*?FACTORS(?<!")\n*([\s\S]*?)\n*ITEM.*?1B

它正在抓取 ITEM 1A. RISK FACTORS 之间的文本和 ITEM 1B. , 但如何才能只抓取超过 5 个字符的捕获组呢?

完整字符串:

ITEM 1A.    RISK FACTORS

123

ITEM 1B.

ITEM 1A.    RISK FACTORS

In addition to other information in this Form 10-K, the following risk factors should be carefully considered in evaluating us and our business because these factors currently have a significant impact or 

ITEM 1B.

因此,所需的捕获组将是:

In addition to other information in this Form 10-K, the following risk factors should be carefully considered in evaluating us and our business because these factors currently have a significant impact or 

而不是:

123

最佳答案

像这样接近数据进行计数。
如果需要,可以显着缩短将 [^\S\r\n] 替换为 \h 的正则表达式。
第 1 组包含修剪后的数据。

(?sm)^[^\S\r\n]*ITEM[^\S\r\n]+1A[^\S\r\n]*\.[^\S\r\n]+风险[^\S\r\n]+因素[^\S\r\n]*\r?\n\s*(\S(?:(?!^[^\S\r\n]*ITEM)){3,}?\S)\s*^[^\S\r\n]*ITEM[^\S\r\n]+1B[^\S\r\n]*\.

https://regex101.com/r/ChQseo/1

展开

 (?sm)
 ^ [^\S\r\n]* ITEM [^\S\r\n]+ 1A [^\S\r\n]* \. 
 [^\S\r\n]+ RISK [^\S\r\n]+ FACTORS [^\S\r\n]* \r? \n 

 \s* 
 (                             # (1 start)
      \S 
      (?:
           (?! ^ [^\S\r\n]* ITEM )
           . 
      ){3,}?
      \S 
 )                             # (1 end)
 \s* 

 ^ [^\S\r\n]* ITEM [^\S\r\n]+ 1B [^\S\r\n]* \.

关于python - 如何确保捕获组超过 5 个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58251914/

相关文章:

javascript - Selenium WebDriver Python重新加载html而不刷新页面

c# - 解析PnPID的正则表达式

java - 具有重复规则的正则表达式

sql - postgresql 是否支持 lookbehind regexp?

c++ - 如何在 Python 中将列表作为 dict 的键?

python - 如何检查 n 列表并将其转换为 Python 中的 pandas 数据框?

python - multiprocessing.Queue 和 Queue.Queue 有什么不同?

关于工作刷新的 python 多处理池通知

javascript - 递归模式js

regex - 防止危险的正则表达式停止应用程序