gpt4 book ai didi

python - 使用 Regex Python 提取字符串中的字符串

转载 作者:太空宇宙 更新时间:2023-11-03 16:30:59 25 4
gpt4 key购买 nike

假设我有这个字符串

“输入:我们可以在拉各斯预订酒店吗?解析:book VB ROOT +-- Can MD aux +-- we PRP nsubj +-- hotel NN dobj | +-- an DT det | +-- in IN准备 | +-- 拉各斯 NNP pobj +-- ? .punct "

我想要一个这样的列表

['book VB ROOT', 'Can MD aux',..., '? . punct']

使用正则表达式。

我尝试过这样做

result = re.findall('\||\+-- (.*?)\+--|\| ', result, re.DOTALL)

如有任何帮助,我们将不胜感激

最佳答案

通过使用内置函数和方法,无需使用正则表达式:

>>> filter(bool, map(str.strip, s.replace('+--', '|').split('Parse:')[1].split('|')))
['book VB ROOT', 'Can MD aux', 'we PRP nsubj', 'hotel NN dobj', 'an DT det', 'in IN prep', 'Lagos NNP pobj', '? . punct']

关于python - 使用 Regex Python 提取字符串中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37614622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com