从python Regex中的给定字符串中提取特定长度的字符

时间:2016-11-30 04:19:20

标签: python regex

您好我有记录,

例如:

Health Insurance PortabilityNEG Ratio
Health Insurance PortabilityNEGRatio
Health Insurance PortabilityNEG NEGRatio

我需要提取PortabilityNEG 我用正则表达式作为

Insurance(.{25}).*?

但是我不想提保险。请告诉我如何根据这一点编写正则表达式?

2 个答案:

答案 0 :(得分:0)

这是您可以从您提供的行中提取所有PortabilityNEG术语的方法。

import re

a="""
Health Insurance PortabilityNEG Ratio
Health Insurance PortabilityNEGRatio
Health Insurance PortabilityNEG NEGRatio
"""
print re.findall('Insurance\s+(PortabilityNEG)',a,re.MULTILINE)

输出:

['PortabilityNEG', 'PortabilityNEG', 'PortabilityNEG']

答案 1 :(得分:0)

由于您不想提及“保险”,您可以尝试以下方法:

# Set up your test string
test_string = """Health Insurance PortabilityNEG Ratio
Health Insurance PortabilityNEGRatio
Health Insurance PortabilityNEG NEGRatio"""

# Set your pattern using regular expression groups
pattern = re.compile("(\w+)\s(\w+)\s(\w{0,14})([\w ]+)")

# Use re.sub to replace all groups with only the third group
[pattern.sub('\\3',x) for x in test_string.split("\n")]

# ['PortabilityNEG', 'PortabilityNEG', 'PortabilityNEG']

我希望这会有所帮助。