Divide string by line break or period with Python regular expressions

后端 未结 5 1292
逝去的感伤
逝去的感伤 2020-12-17 10:31

I have a string:

\"\"\"Hello. It\'s good to meet you.
My name is Bob.\"\"\"

I\'m trying to find the best way to split this into a list divi

5条回答
  •  清酒与你
    2020-12-17 11:07

    You don't need regex.

    >>> txt = """Hello. It's good to meet you.
    ... My name is Bob."""
    >>> txt.split('.')
    ['Hello', " It's good to meet you", '\nMy name is Bob', '']
    >>> [x for x in map(str.strip, txt.split('.')) if x]
    ['Hello', "It's good to meet you", 'My name is Bob']
    

提交回复
热议问题