0

I have a string of this type:

s = "//name#1  un2 //name2#1  u13 //name3#3"

How can I get these arrays of strings:

s1 [] = "//name, //name2, name3"
s2 [] = "1, 2, 3"
1
  • Will your data always look like this?
    – wkl
    Apr 19, 2012 at 18:54

2 Answers 2

2

This does it with just one regular expression:

>>> 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'
1
  • @dgh: I though yours was better than mine :-)
    – Abhijit
    Apr 19, 2012 at 19:16
0

Probably you are looking for

>>> s1=", ".join(re.findall("(//.*?)#\d+",s))
>>> s2=", ".join(re.findall("#(\d+)",s))
>>> s1
'//name, //name2, //name3'
>>> s2
'1, 1, 3'
>>> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.