【发布时间】:2021-05-19 13:57:19
【问题描述】:
我在一个字符串中有一些来自 pdf 的文本,我想将其分解,以便我有一个列表,其中每个字符串都以数字和句点开头,然后在下一个数字之前停止。
比如我想转这个:
'3.1 First liens 15,209,670,396 0 15,209,670,396 14,216,703,858
3.2 Other than first liens 0 0
4. Real estate:
4.1 Properties occupied by the company (less $ 43,332,898
encumbrances) 68,122,291 0 68,122,291 64,237,046
4.2 Properties held for the production of income (less
$ encumbrances) 0 0
4.3 Properties held for sale (less $
encumbrances) 0 0
5. Cash ($ (101,130,138)), cash equivalents
($ 850,185,973 ) and short-term
investments ($ 0 ) 749,055,835 0 749,055,835 1,867,997,055
6. Contract loans (including $ premium notes) 253,533,676 0 253,533,676 233,680,271
7. Derivatives 3,194,189,871 0 3,194,189,871 2,390,781,023
8. Other invested assets 749,074,191 11,899,360 737,174,831 692,916,503'
进入这个:
['3.1 First liens 15,209,670,396 0 15,209,670,396 14,216,703,858 ',
'3.2 Other than first liens 0 0 ',
'4. Real estate:',
'4.1 Properties occupied by the company (less $ 43,332,898 encumbrances) 68,122,291 0 68,122,291 64,237,046',
'4.2 Properties held for the production of income (less $ encumbrances) 0 0'
'4.3 Properties held for sale (less $ encumbrances) 0 0',
'5. Cash ($ (101,130,138)), cash equivalents ($ 850,185,973 ) and short-term investments ($ 0 )
749,055,835 0 749,055,835 1,867,997,055',
'6. Contract loans (including $ premium notes) 253,533,676 0 253,533,676 233,680,271',
'7. Derivatives 3,194,189,871 0 3,194,189,871 2,390,781,023',
'8. Other invested assets 749,074,191 11,899,360 737,174,831 692,916,503']
问题是原始字符串在标题中间散布了“\n”(例如,在 4.1 中,单词 encumbrances 之前有一个 \n。
(\d+\.[\s\S]*(?!\d+\.))
这是我一直在尝试使用的正则表达式,但它匹配整个字符串而不是每个数字行。有没有办法让我的正则表达式在下一个数字行之前停止匹配?
【问题讨论】:
-
你确定没有出现其他的期数,除了你指导打破它的那些吗
-
@FrankSiret 我刚刚再次扫描了字符串,但没有看到任何内容,所以我很确定。
-
它看起来像
\d+\.[\s\S]*?(?=\s*\d+\.|\Z)或\d+\.[\s\S]*?(?=\n\d+\.|\Z)的案例
标签: python regex regex-lookarounds regex-greedy