【问题标题】:Python Regular Expression - Get Text starting in the next line after the match was foundPython 正则表达式 - 在找到匹配项后从下一行开始获取文本
【发布时间】:2022-11-25 01:57:38
【问题描述】:

我有一个关于在 Python 中使用正则表达式的问题。这是我正在分析的文本的一部分。

Amit Jawaharlaz Daryanani,  Evercore ISI Institutional Equities, Research Division - Senior MD & Fundamental Research Analyst   [19]\n I have 2 as well. I guess, first off, on the channel inventory, I was hoping if you could talk about how did channel inventory look like in the March quarter because it sounds like it may be below the historical ranges. And then the discussion you had for June quarter performance of iPhones, what are you embedding from a channel building back inventory levels in that expectation?\n 

我的目标是通过匹配分析师的姓名 Amit Jawaharlaz Daryanani 来提取这部分文本:\n 我也有 2 个。我想,首先,关于渠道库存,我希望你能谈谈 3 月份季度的渠道库存情况如何,因为听起来它可能低于历史范围。然后是您对 iPhone 6 月季度业绩的讨论,您从渠道构建的库存水平中嵌入了什么?\n

我不能只从 \n 到 \n,因为文本要长得多,而且我特别需要他名字后面的文本行。

我试过了: re.findall(r'(?<=Amit Jawaharlaz Daryanani).*?(?=\n)', text)

但是这里的输出是

[',  Evercore ISI Institutional Equities, Research Division - Senior MD & Fundamental Research Analyst   [19]'

那么我怎样才能从他名字后面的第一个 \n 到他名字后面的第二个 \n 之后开始呢?

【问题讨论】:

    标签: python regex


    【解决方案1】:

    您可以使用捕获组:

    Amit Jawaharlaz Daryanani.*
    s*([^.]+.)
    

    regex demo

    【讨论】:

      【解决方案2】:

      尝试这个:

      • 名称的非捕获组
      • 寻找第一个
      • 捕获组直到第二个
      re.findall(r'(?:Amit Jawaharlaz Daryanani).*?
      (.*?)
      ', text)
      

      这是因为 .*? 是一个非贪婪组。这意味着它会在遇到第一个 之前停止。

      输出:

      [' I have 2 as well. I guess, first off, on the channel inventory, I was hoping if you could talk about how did channel inventory look like in the March quarter because it sounds like it may be below the historical ranges. And then the discussion you had for June quarter performance of iPhones, what are you embedding from a channel building back inventory levels in that expectation?']
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-06
        • 2011-06-27
        • 1970-01-01
        • 2019-09-22
        相关资源
        最近更新 更多