How to get the first word in the string

后端 未结 6 825
梦毁少年i
梦毁少年i 2020-12-05 04:07

text is :

WYATT    - Ranked # 855 with    0.006   %
XAVIER   - Ranked # 587 with    0.013   %
YONG     - Ranked # 921 with    0.006   %
YOUNG    - Ranked # 8         


        
6条回答
  •  無奈伤痛
    2020-12-05 04:18

    You don't need regex to split a string on whitespace:

    In [1]: text = '''WYATT    - Ranked # 855 with    0.006   %
       ...: XAVIER   - Ranked # 587 with    0.013   %
       ...: YONG     - Ranked # 921 with    0.006   %
       ...: YOUNG    - Ranked # 807 with    0.007   %'''
    
    In [2]: print '\n'.join(line.split()[0] for line in text.split('\n'))
    WYATT
    XAVIER
    YONG
    YOUNG
    

提交回复
热议问题