我正在使用这段代码:
(?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/