Python-get string between to characters

后端 未结 4 430
悲哀的现实
悲哀的现实 2021-01-29 14:07

I need to one give me the string between ~ and ^.
I have a string like this:

~~~~ ABC ^ DEF ^ HGK > LMN ^  

I

4条回答
  •  不思量自难忘°
    2021-01-29 14:21

    Without regex:

    >>> "".join([x for x in target if x.isalpha() or x == ' ']).split()
    ['ABC', 'DEF', 'HGK', 'LMN']
    

    This takes space and alpha characters and creates a new string then splits it into words in a list

    Here is my exact code from python 3 command line:

    >>> target = ' ~~~~ ABC ^ DEF ^ HGK > LMN ^  '
    >>> xx = "".join([x for x in target if x.isalpha() or x == ' ']).split()
    >>> xx
    ['ABC', 'DEF', 'HGK', 'LMN']
    >>> 
    

提交回复
热议问题