Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Divide string by line break or period with Python regular expressions

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.

like image 889
David Y. Stephenson Avatar asked Jul 12 '13 15:07

David Y. Stephenson


People also ask

How do you split a string in regular expression in Python?

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.

How do you split a string by period in Python?

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.

Can we split string using regex?

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.


2 Answers

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']
like image 134
falsetru Avatar answered Oct 15 '22 10:10

falsetru


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...

like image 42
Tim Pietzcker Avatar answered Oct 15 '22 11:10

Tim Pietzcker