簡體   English   中英

從python Regex中的給定字符串中提取特定長度的字符

[英]Extraction of characters of particular length from a given string in python Regex

嗨,我有類似的記錄,

例如:

Health Insurance PortabilityNEG Ratio
Health Insurance PortabilityNEGRatio
Health Insurance PortabilityNEG NEGRatio

在這里我需要提取PortabilityNEG我使用了正則表達式作為

Insurance(.{25}).*?

但是我不想提保險。請讓我知道如何根據此編寫正則表達式?

這是您可以從給定的行中提取所有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']

由於您不想提及“保險”,因此可以嘗試以下操作:

# 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']

我希望這有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM