Python (re)

Sentence Boundary in PY

Matches sentence boundaries (punctuation followed by whitespace and a capital letter).

Try it in the PY tester →

Pattern

regexPY
[.!?]\s+(?=[A-Z])   (flags: g)

Python (re) code

pyPython
import re

pattern = re.compile(r"[.!?]\s+(?=[A-Z])")
input_text = "Hello world. How are you? I'm fine!"
for m in pattern.finditer(input_text):
    print(m.group(0))

Stdlib `re` module — no third-party dependency. Works on Python 3.6+.

How the pattern works

`[.!?]` matches terminating punctuation. `\s+` matches whitespace. `(?=[A-Z])` is a lookahead for a capital letter (marks where the next sentence begins).

Examples

Input

Hello world. How are you? I'm fine!

Matches

  • .
  • ?

Same pattern, other engines

← Back to Sentence Boundary overview (all engines)