Sentence Splitting with Regex

Using regular expressions to split text into sentences, handling various edge cases

Python
import re

def split_sentences(text):
    pattern = r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?|\!)\s'
    return re.split(pattern, text)

sample_text = "Hello, world! This is a test. Mr. Smith went to Washington D.C. Is this working?"
sentences = split_sentences(sample_text)
print("Sentences:")
for i, sentence in enumerate(sentences, 1):
    print(f"{i}. {sentence}")
Click Run or press shift + ENTER to run code.