I have a form (a string) that I want to process. The form can contain certain occurrences of this, for example:
I need to grab every number after every occurrence of
>>, but I'm not sure how. I'm thinking regex, but I'm terrible at it. I've read several similar questions, but the answers are wildly different, don't apply to this (they find the next WORD after a keyword) or sometimes use contradicting approaches.
What's the best way to do this? Thanks.
You can use
findall() with a positive look behind:
>>> import re >>> >>> s = ">>1244" >>> re.findall(r"(?<=>>)\d+", s) ['1244'] >>> >>> s = ">>1244 something >>500" >>> re.findall(r"(?<=>>)\d+", s) ['1244', '500']
(?<=>>)\d+ expression would match one or more digits (
\d+) that go after the
You can use regular expressions, try:
s = ">>1244 aaa >>112 >>344>>456 aaa sss >>345 >>aa 567" import re result = re.findall(">>(\d+)",s) print result
['1244', '112', '344', '456', '345']
Remember that findall return a list of groups, if there are groups. You indicate groups using '(' and ')' to initiate and terminate respectively.
If it's that simple to where you're only looking for a single occurance of
>> and wanting to grab everything after that substring, you could do this:
mystring = "1234>>5678" after = mystring.split(">>")
That would split the string into a list at the ">>" and then grab the 2nd element of the list which would be everything after the ">>".