问题描述:

I have a form (a string) that I want to process. The form can contain certain occurrences of this, for example: >>1244.

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']

Here the (?<=>>)\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

Output:

['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.

Look: https://docs.python.org/2/library/re.html

网友答案:

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(">>")[1] 

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 ">>".

相关阅读:
Top