使用regex分离文本/文本处理

2024-05-17 04:04:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个段落需要用一个特定的关键字列表来分隔。你知道吗

以下是文本(单个字符串):

"Evaluation Note: Suspected abuse by own mother. Date 3/13/2019 ID: #N/A Contact: Not Specified Name: Cecilia Valore Address: 189 West Moncler Drive Home Phone: 353 273 400 Additional Information: Please tell me when the mother arrives, we will have a meeting with her next Monday, 3/17/2019 Author: social worker"

所以我想用python根据变量名来分隔这一段。”Evaluation Note、Date、ID、Contact、Name、Address、Home Phone、Additional Information和Author是变量名。我觉得使用regex看起来不错,但是我没有很多regex的经验。你知道吗

以下是我想做的:

import re

regex = r"Evaluation Note(?:\:)? (?P<note>\D+) Date(?:\:)? (?P<date>\D+)
ID(?:\:)? (?P<id>\D+) Contact(?:\:)? (?P<contact>\D+)Name(?:\:)? (? P<name>\D+)"

test_str = "Evaluation Note: Suspected abuse by own mother. Date 3/13/2019
ID: #N/A Contact: Not Specified Name: Cecilia Valore "

matches = re.finditer(regex, test_str, re.MULTILINE)

但没有找到任何模式。你知道吗


Tags: namereiddatebynotcontactregex
2条回答

您可以使用搜索来获取变量的位置并相应地解析文本。你可以很容易地定制它。你知道吗

import re
en = re.compile('Evaluation Note:').search(text)
print(en.group())
d = re.compile('Date').search(text)
print(text[en.end()+1: d.start()-1])
print(d.group())
i_d = re.compile('ID:').search(text)
print(text[d.end()+1: i_d.start()-1])
print(i_d.group())
c = re.compile('Contact:').search(text)
print(text[i_d.end()+1: c.start()-1])
print(c.group())
n = re.compile('Name:').search(text)
print(text[c.end()+1: n.start()-1])
print(n.group())
ad = re.compile('Address:').search(text)
print(text[n.end()+1: ad.start()-1])
print(ad.group())
p = re.compile('Home Phone:').search(text)
print(text[ad.end()+1: p.start()-1])
print(p.group())
ai = re.compile('Additional Information:').search(text)
print(text[p.end()+1: ai.start()-1])
print(ai.group())
aut = re.compile('Author:').search(text)
print(text[ai.end()+1: aut.start()-1])
print(aut.group())
print(text[aut.end()+1:])

这将输出:

Evaluation Note: Suspected abuse by own mother.

Date: 3/13/2019

ID: #N/A

Contact: Not Specified

Name: Cecilia Valore

Address: 189 West Moncler Drive

Home Phone: 353 273 400

Additional Information: Please tell me when the mother arrives, we will have a meeting with her next Monday, 3/17/2019

Author: social worker

我希望这有帮助

您可能可以动态生成正则表达式。只要参数的顺序是固定的。你知道吗

在这里我尝试一下,它确实起作用了。它所追求的实际正则表达式类似于Some Key(?P<some_key>.*)Some Other Key(?P<some_other_key>.*),以此类推。你知道吗

import re

test_str = r'Evaluation Note: Suspected abuse by own mother. Date 3/13/2019 ID: #N/A Contact: Not Specified Name: Cecilia Valore '
keys = ['Evaluation Note', 'Date', 'ID', 'Contact', 'Name']

def find(keys, string):
    keys = [(key, key.replace(' ', '_')) for key in keys] # spaces aren't valid param names
    pattern = ''.join([f'{key}(?P<{name}>.*)' for key, name in keys]) # generate the actual regex
    for find in re.findall(pattern, test_str):
        for item in find:
            yield item.strip(':').strip() # clean up the result

for find in find(keys, test_str):
    print(find)

返回:

Suspected abuse by own mother.
3/13/2019
#N/A
Not Specified
Cecilia Valore

相关问题 更多 >