Checking Prefixes and Suffixes of Text

A prefix must start at the first position, and a suffix must reach the last. A general substring can occur anywhere.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Use filenames carefully
  2. Agree on case and boundary
  3. Check exact relationships between two strings
  4. Check empty candidates and transformations

Use filenames carefully

The string "notes.csv.old" contains ".csv" and does not end with it. A suffix predicate expresses extension matching when that path convention suffices. Substring and subsequence rules clarify which positions are required.

Agree on case and boundary

A program may match "NOTES.CSV" after folding case. If a bare name "csv" should fail, include the dot in the expected suffix. Case-insensitive text comparisons explain why both the policy and the normalization need definition.[1]

Check exact relationships between two strings

For "build.tar.gz", the suffix ".gz" matches and so does ".tar.gz". A parser choosing between meaningful extensions should specify whether the longest supported match wins. Searching for ".gz" somewhere in "build.gz.tmp" would find text that is not a suffix. Test a name with the sequence in the middle and one with multiple eligible suffixes. See substring versus subsequence for related sequence relations.

Check empty candidates and transformations

The empty string is both a prefix and a suffix of every string under ordinary string semantics. If an empty configured prefix should be rejected, validate the configuration before filtering. Where suffix matching is case insensitive, decide on the comparison policy before removing anything. A normalized comparison length can differ from the raw string length. See prefix removal versus stripping and normalization.

References

  1. Python Software Foundation, Python Standard Library: Built-in Types ↩