How can I match text up to the next delimiter without crossing it?
Quick answer
For a semicolon-delimited field, use ([^;]*);: [^;]* matches zero or more characters other than ;, and the explicit semicolon requires a delimiter after the field. It permits an empty field; use + instead of * to require at least one character. Anchor or otherwise position the pattern at the field’s start so it doesn’t find a later field unexpectedly. [S1]
The question, the details, the next step.
A practical checklist
Choose whether empty fields are valid: use
*to allow them or+to require content.Include the delimiter in the pattern when its presence must be required.
Anchor at the field’s starting position, or deliberately define how matching finds that position.
Test a normal field, an empty field, adjacent delimiters, and input with no delimiter.
Check whether fields can contain quoted or escaped delimiters; this simple pattern does not handle those cases.
A few more useful answers
Why not use `.*;`?
A broad wildcard can consume delimiter characters and extend farther than the next delimiter. A negated character class such as [^;]* cannot consume a semicolon, so it stops before one. [S1]
Does `[^;]*` by itself confirm that a semicolon exists?
No. It can match zero or more non-semicolon characters, including an empty string. Match the delimiter explicitly, as in ([^;]*);, if its presence is required. [S2]
How do I reject an empty field?
Use + instead of *, as in ([^;]+);. The + requires at least one character other than the semicolon. [S1]
Sources
References used for this guide.
- [S1] Python 3 regular-expression operations: character sets and repetition docs.python.org · Checked
- [S2] MDN: JavaScript character classes developer.mozilla.org · Checked
