提取两个字符串之间的多个字符串

时间:2020-10-19 10:49:44

标签: python string

我希望能够在两个字符串之间提取一个字符串

输入:

one_string = " ‘name’:’john’,’phone’:’8947467’,’name’:’anthony’,’phone’:’74729’ "

Names are between strings: ‘name’:’ and ’

Phones are between strings: ’phone’:’ and ’

输出:

names = 'john','anthony'
phones = '8947467','74729'

有什么想法吗?这可能很容易,但我迷路了。

谢谢

2 个答案:

答案 0 :(得分:4)

使用where-object { -not $_.PSIsContainer }

re.findall

此打印:

# -*- coding: utf-8 -*-

one_string = " ’name’:’john’,’phone’:’8947467’,’name’:’anthony’,’phone’:’74729’ "
names = re.findall(r'’name’:’(.*?)’', one_string)
phones = re.findall(r'’phone’:’(.*?)’', one_string)
print("names =  " + ','.join(names))
print("phones = " + ','.join(phones))

注意:您的示例输入似乎并不总是使用一致的弯引号。我认为最合理的答案是我已解决的问题。

答案 1 :(得分:0)

效果很好,谢谢