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 divided by periods and linebreaks:
["Hello", "It's good to meet you", "My name is Bob"]
I'm pretty sure I should use regular expressions, but, having no experience with them, I'm struggling to figure out how to do this.
If you want to split a string that matches a regular expression (regex) instead of perfect match, use the split() of the re module. In re. split() , specify the regex pattern in the first parameter and the target character string in the second parameter.
In Python you can split a string with the split() method. It breaks up a string (based on the given separator) and returns a list of strings.
split(String regex) method splits this string around matches of the given regular expression. This method works in the same way as invoking the method i.e split(String regex, int limit) with the given expression and a limit argument of zero. Therefore, trailing empty strings are not included in the resulting array.
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']
For your example, it would suffice to split on dots, optionally followed by whitespace (and to ignore empty results):
>>> s = """Hello. It's good to meet you.
... My name is Bob."""
>>> import re
>>> re.split(r"\.\s*", s)
['Hello', "It's good to meet you", 'My name is Bob', '']
In real life, you'd have to handle Mr. Orange
, Dr. Greene
and George W. Bush
, though...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With