我有一个字符串string
string = "get|Server ID:Server1,Server ID:Server2,Server ID:Server3,Query:SELECT * FROM sometable,_xsrf:57bsdkfnklbjhgfhjfghsndfsdfsdf"
get|
和,Query:
之间的唯一内容是服务器ID
在我的示例中有3个服务器ID,但可以有更多
string = "get|Server ID:Server1,Server ID:Server2,Server ID:Server3,Server ID:Server4,Server ID:Server5,Query:SELECT * FROM sometable,_xsrf:57bsdkfnklbjhgfhjfghsndfsdfsdf"
我想选择所有服务器ID,例如server1,server2,server3等,并将它们放在列表中。
答案 0 :(得分:4)
试试这个:
text = "get|Server ID:Server1,Server ID:Server2,Server ID:Server3...,Query:SELECT * FROM sometable,_xsrf:57bsdkfnklbjhgfhjfghsndfsdfsdf"
pattern = r'Server ID:([^,.]*),?'
result = re.findall(pattern, text)
print result
这是输出:
['Server1', 'Server2', 'Server3']