pysnippets.strings.words
Split a string into words
words(text: str, pattern: str = DEFAULT_WORD) -> list[str]
Extract the words from text with a regex, keeping apostrophes and hyphens inside words.
text.split() keeps punctuation attached: 'Python!!'. A regex picks out the words alone.
words.py✓ ruff · mypy --strict
import re
DEFAULT_WORD = r"[^\W\d_]+(?:['-][^\W\d_]+)*"
def words(text: str, pattern: str = DEFAULT_WORD) -> list[str]:
"""Return the words in ``text``, as matched by ``pattern``."""
return re.findall(pattern, text)- letters, optionally joined by ' or -, so "don't" stays one word
examples✓ 3 doctests · 3.11–3.14
passed on Python 3.11 to 3.14: >>> words("I love Python!!")['I', 'love', 'Python']passed on Python 3.11 to 3.14: >>> words("don't stop-motion, café & 42")["don't", 'stop-motion', 'café']passed on Python 3.11 to 3.14: >>> words("build -q --out one-item", r"\b[a-zA-Z-]+\b")['build', 'q', 'out', 'one-item']Edit the function or the examples and run the doctests here, on Python 3.14. Python loads only when you click (about 7 MB, cached after the first time).
python 3.14 · in your browser
Loading Python 3.14 in your browser (≈7 MB, cached after the first time)…
Tab inserts four spaces. Press Escape, then Tab, to leave the editor. Control or Command plus Enter runs the examples.
How it works
[^\W\d_]means a letter: not a non-word character, not a digit, not an underscore. It matcheséand other non-ASCII letters too.(?:['-]...)*letsdon'tandstop-motionstay one word.- Pass your own pattern to change what counts as a word.
Complexity: O(n) time, O(n) space
- 3.11✓passes
- 3.12✓passes
- 3.13✓passes
- 3.14✓passes
Import
from pysnippets.strings.words import wordsNo dependencies: copy words.py into your project, or clone the repo.Adapted from words in 30-seconds-of-python by the 30 seconds of code contributors, CC BY 4.0. Changed: Unicode-aware default pattern that keeps apostrophes; typed. Suggest an edit.