python find substrings based on a delimiter

后端 未结 4 1897
离开以前
离开以前 2021-01-17 17:54

I am new to Python, so I might be missing something simple.

I am given an example:

 string = \"The , world , is , a , happy , place \" 
4条回答
  •  不要未来只要你来
    2021-01-17 18:22

    Another option:

    import re
    
    string = "The , world , is , a , happy , place "
    match  = re.findall(r'[^\s,]+', string)
    for m in match:
        print m
    

    Output

    The
    world
    is
    a
    happy
    place
    

    See a demo

    You could also just use match = re.findall(r'\w+', string) and you will get the same output.

提交回复
热议问题