通过Tag Python加入全文

问题描述 投票:-1回答:2

让我说我有这样的句子:

His/O name/O is/O Petter/Name Jack/Name and/O his/O brother/O name/O is/O 
Jonas/Name Van/Name Dame/Name

我怎样才能得到这样的结果:

Petter Jack, Jonas Van Dame.

到目前为止,我已经尝试过这个,但它仍然只是加入2个字:

import re
pattern = re.compile(r"\w+\/Name)
sent = sentence.split()
for i , w in sent:
    if pattern.match(sent[i]) != None:
        if pattern.match(sent[i+1]) != None:
        #....
        #join sent[i] and sent[i+1] element
        #....
python regex string
2个回答
1
投票

尝试这样的事情

pattern = re.compile(r"((\w+\/Name\s*)+)")
names = pattern.findall(your_string)
for name in names:
    print(''.join(name[0].split('/Name')))

1
投票

我正在考虑一个两阶段的解决方案

r = re.compile(r'\w+\/Name(?:\ \w+\/Name)*')    
result = r.findall(s)
# -> ['Petter/Name Jack/Name', 'Jonas/Name Van/Name Dame/Name']

for r in result:
    print(r.replace('/Name', ''))
# -> Petter Jack
# -> Jonas Van Dame
© www.soinside.com 2019 - 2024. All rights reserved.