我有一个这种类型的字符串:
s = "//name#1 un2 //name2#1 u13 //name3#3"
我怎样才能得到这些字符串数组:
s1 [] = "//name, //name2, name3"
s2 [] = "1, 2, 3"
我有一个这种类型的字符串:
s = "//name#1 un2 //name2#1 u13 //name3#3"
我怎样才能得到这些字符串数组:
s1 [] = "//name, //name2, name3"
s2 [] = "1, 2, 3"
只需一个正则表达式即可:
>>> s = "//name#1 un2 //name2#1 u13 //name3#3"
>>> import re
>>> x = re.findall(r"(\S+)#(\S+)", s)
>>> [s1, s2] = [', '.join(y) for y in zip(*x)]
>>> s1
'//name, //name2, //name3'
>>> s2
'1, 1, 3'
可能你正在寻找
>>> s1=", ".join(re.findall("(//.*?)#\d+",s))
>>> s2=", ".join(re.findall("#(\d+)",s))
>>> s1
'//name, //name2, //name3'
>>> s2
'1, 1, 3'
>>>