【问题标题】:How to make my regex match stop after a lookahead?如何让我的正则表达式匹配在前瞻后停止?
【发布时间】: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


【解决方案1】:

循环浏览找到的每个捕获组:

^[\']?(?=[\d].)[\d].[\d]*([\s\w\,\:\(\)\$\-]*)[\']?[ ]*(\n|\Z)

【讨论】:

    【解决方案2】:

    类似:

    list = re.findall(r"^\d+\..*?(?=^\d+\.|\Z)", text, re.MULTILINE | re.DOTALL)

    应要求提供进一步解释。

    【讨论】:

    • 这很好用!! @MikeM 你是大师
    • @FrankSiret 谢谢,弗兰克。你真是太慷慨了。
    • 感谢大家的帮助!这是我最终使用的方法,效果很好。
    【解决方案3】:
    import re
    
    txt = '''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'''
    
    x = re.split('[0-9]+\.[0-9]*', txt)
    y = re.findall('[0-9]+\.[0-9]*', txt)
    z = []
    
    for i in range(len(y)):
        t = y[i]+x[i+1]
        z.append(t)
    
    print(z)
    

    如果需要空格换行,只需要替换

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-22
      • 2015-08-22
      • 2015-09-13
      • 2021-11-15
      • 2020-04-15
      • 1970-01-01
      相关资源
      最近更新 更多