【问题标题】:Extract text between two substrings using regular expression multiline in python在python中使用正则表达式多行提取两个子字符串之间的文本
【发布时间】:2020-03-22 08:44:51
【问题描述】:

我想使用 python 使用正则表达式提取两个子字符串或短语之间的文本。

示例文本:

NAME – Testing set ADDRESS – 1470 ROAD CONTACT NUMBER - +91-44578558774 E-MAIL – test.side@gmail.com 


PROFESSIONAL PROFILE 

A petroleum graduate with professional experience in workover operation & Surface Well Testing operation and implementation of procedures and best practices following knowledge management processes. Seeking a role in oil and gas industry to develop engineering and management skills and apply new ideas to real life industry problem. 


PROFESSIONAL EXPERIENCE 

RIG (JUN 2014 – SEPT 2015)  

Performed various workover operations in SRP and ESP wells developed in Ahmedabad (ONGC Project) & Durgapur (ESSAR Project). My responsibilities as Roustabout on Rig was to perform:  Make pipe connection & Operate Tongs while running in and Pull out of Sucker rod & Tubings.

CORE COMPETENCIES: 

1. Well versed with varioussoftware for well testing (Wireless software). 2. Good Knowledge of MS-EXCEL, MS-Word.

正则表达式用于提取 PROFESSIONAL PROFILE核心竞争力? 我无法正确使用正则表达式。

【问题讨论】:

  • 也许:PROFESSIONAL PROFILE([\s\S]+)CORE COMPETENCIES: 将其放入第 1 组 (\1)。最好发布尝试并要求改进或您发现不起作用,而不仅仅是说明所需的任务;o)
  • 我尝试了相同的表达式,但没有得到输出。抱歉错过了发布我的尝试
  • 我会发布一个答案来显示一些代码,因为在 cmets 中显示太棘手了

标签: python regex


【解决方案1】:

从 cmets 继续...试试这个作为演示...

import re

pattern = re.compile(r'PROFESSIONAL PROFILE([\s\S]+)CORE COMPETENCIES:')

data = '''
NAME – Testing set ADDRESS – 1470 ROAD CONTACT NUMBER - +91-44578558774 E-MAIL – test.side@gmail.com 


PROFESSIONAL PROFILE 

A petroleum graduate with professional experience in workover operation & Surface Well Testing operation and implementation of procedures and best practices following knowledge management processes. Seeking a role in oil and gas industry to develop engineering and management skills and apply new ideas to real life industry problem. 


PROFESSIONAL EXPERIENCE 

RIG (JUN 2014 – SEPT 2015)  

Performed various workover operations in SRP and ESP wells developed in Ahmedabad (ONGC Project) & Durgapur (ESSAR Project). My responsibilities as Roustabout on Rig was to perform:  Make pipe connection & Operate Tongs while running in and Pull out of Sucker rod & Tubings.

CORE COMPETENCIES: 

1. Well versed with varioussoftware for well testing (Wireless software). 2. Good Knowledge of MS-EXCEL, MS-Word.

'''

#print(pattern.search(data)[1])
#I think the  is tripping the code up so consider...
print(pattern.search(data)[1].encode('utf8'))

输出:

A petroleum graduate with professional experience in workover operation & Surface Well Testing operation and implementation of procedures and best practices following knowledge management processes. Seeking a role in oil and gas industry to develop engineering and management skills and apply new ideas to real life industry problem. 


PROFESSIONAL EXPERIENCE 

RIG (JUN 2014 – SEPT 2015)  

Performed various workover operations in SRP and ESP wells developed in Ahmedabad (ONGC Project) & Durgapur (ESSAR Project). My responsibilities as Roustabout on Rig was to perform:  Make pipe connection & Operate Tongs while running in and Pull out of Sucker rod & Tubings.

【讨论】:

  • 简单的答案,但在我的情况下完美运行......只是想给你投票,因为它解决了我认为是一个复杂的问题......regex101.com/r/smxV0v/1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-15
  • 1970-01-01
  • 1970-01-01
  • 2018-04-04
  • 2012-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多