Reputation: 2534
If I have a string like this:
string = "12345|67891|23456|123456?"
how would I take out the "12345" and the "67891", etc (the characters between the pipes) and add them to a list all the way until the question mark (I am using the question mark in my code as a terminating character)?
A similar question has been asked here: How do I find the string between two special characters?
but I think mine is different because I need to do it multiple times in the same, one-line string.
Here is what I am hoping to achieve:
[PROGRAM BEGINS]
>>>string = "12345|67891|23456|123456?"
>>>string_list = []
>>>#some code to extract it and add it to a list called string_list
>>>print string_list
["12345","67891","23456","123456"]
[PROGRAM TERMINATES]
Thanks in advance!!
Upvotes: 0
Views: 1137
Reputation: 94
Considering you are using '?' as a terminating char. The safest way to do this would be:
>>> string = "12345|67891|23456|123456?"
>>> string.split('?')[0].split('|')
['12345', '67891', '23456', '123456']
Upvotes: 1
Reputation: 304443
If the question mark is always at the end.
>>> string = "12345|67891|23456|123456?"
>>> string.rstrip('?').split('|')
['12345', '67891', '23456', '123456']
regex are relatively slow for performing tasks like this
Upvotes: 2
Reputation: 41
You can use the split function. Str.split("|") and assign the result to an array variable.
Upvotes: 1
Reputation: 6581
You don't need to use the question mark:
>>> string = "12345|67891|23456|123456"
>>> string.split('|')
['12345', '67891', '23456', '123456']
>>>
Upvotes: 1
Reputation: 10961
You can do it with re
module, this way:
>>>import re
>>>s = "12345|67891|23456|123456?"
>>>string_list = re.findall(r'\d+',s)
>>>string_list
['12345', '67891', '23456', '123456']
Upvotes: 1
Reputation: 53545
You can use regex to split on anything which is not a digit \D
:
import re
matches = filter(None, re.split('\D', "12345|67891|23456|123456?"))
print matches # ['12345', '67891', '23456', '123456']
Upvotes: 1