Skip to content

Instantly share code, notes, and snippets.

@gboeer
Created December 1, 2023 10:51

Revisions

  1. gboeer created this gist Dec 1, 2023.
    6 changes: 6 additions & 0 deletions substring_idx.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    import re

    def substring_idx(text, substring):
    """Return the starting indices of all occurrences of substring in text."""
    pattern = re.escape(substring)
    return [match.start() for match in re.finditer(pattern, text)]