Why does this regex result in four items?

后端 未结 3 1935
粉色の甜心
粉色の甜心 2021-01-24 03:44

I want to split a string by , ->, =>, or those wrapped with several spaces, meaning that I can get two items, she and <

3条回答
  •  滥情空心
    2021-01-24 04:22

    If you can just strip your input string. From your description, all you need is to split the words on either \s+ or \s*->\s* or \s*=>\s*

    So here is my solution:

    p = re.compile(r'\s*[-=]>\s*|\s+')
    input1 = "she he"
    input2 = " she  -> he \n".strip()
    
    print p.split(input1)
    print p.split(input2)
    

    Your output would be just 'she' and 'he':

    ['she', 'he']
    

提交回复
热议问题