Report an invalid repeated separator with finditer

Delimited text can contain an accidental doubled separator even when the surrounding fields look plausible. This example treats adjacent identical colons or semicolons as invalid. The pattern captures one separator in the named separator group and then uses \1+ to require one or more immediate repetitions of that same character. Therefore :: and ;; are reported, while a single ; is not.

Pattern.finditer yields match objects rather than only matched strings. Each object supplies the source text through group() and zero-based, end-exclusive offsets through start() and end(). The assertions establish the expected runs and offsets for this concrete input before the reporting loop formats each issue. End-exclusive offsets are convenient for slicing: text[start:end] retrieves the reported run exactly.

This is a narrow validator, not a complete parser for a delimiter-based format. It deliberately does not reject mixed adjacent separators such as :;, separators inside quoted fields, or semantic problems such as a missing required field. Define those rules separately if the format needs them. finditer is long-standing in Python’s re module; the f-strings in the displayed program require Python 3.6+.

Reference: Python re.finditer documentation and the regular-expression syntax reference.

AI-assistance disclosure: This article was drafted with AI assistance and should be reviewed in its application context.

import re

text = "name::value;ok;;done"
pattern = re.compile(r"(?P<separator>[:;])\1+")
problems = list(pattern.finditer(text))

assert [match.group() for match in problems] == ["::", ";;"]
assert [match.span() for match in problems] == [(4, 6), (14, 16)]

for match in problems:
    print(
        f"separator={match['separator']!r} "
        f"start={match.start()} end={match.end()}"
    )
separator=':' start=4 end=6
separator=';' start=14 end=16

Comments

Popular posts from this blog

Compare Two Text Files in Python Without Modifying Them

Seven CSV Quality Checks to Run Before Importing Data

Explain why SQLite transactions cannot make an HTTP call atomic