Regex to stop at the first match (lazy quantifiers and first occurrence)
Codemia Knowledge Hub ·
Use a single-match function (such as Python's re.search) to get only the first match, and use a lazy quantifier like .*? or a negated class like [^"]* to make one match stop at the first delimiter instead of the last. These are two different problems: the first is controlled by the search API, the second by the pattern.
Getting only the first occurrence
Most languages already stop after the first match when you call a single-match function.
Python example:
This returns only id=12.
If you call findall or iterate over matches, you asked for all matches. The pattern is not the problem; the API choice is.
Stopping at the first terminator with a lazy quantifier
Now say you want text from a up to the first b, not the last b.
A greedy pattern:
This matches axxxbxxaxxb because .+ is greedy and consumes as much as possible.
A lazy pattern fixes it:
This returns axxxb, because .+? stops at the earliest b that still lets the pattern succeed.
Negated character classes instead of dot-star
A negated character class or delimiter-aware expression is often better than a lazy quantifier. For quoted text:
Both return "one", but the second is clearer because it says "read characters until the next quote." It also reduces backtracking.
Matching the first div block
The lazy quantifier stops at the first closing tag that satisfies the pattern.
HTML is not a regular language in the practical parser sense, so regex is only safe for very controlled snippets. If the structure can nest or vary significantly, use a real parser.
Stopping before a character with lookahead
To stop before a delimiter without consuming it, use a lookahead:
This returns name=alice. The semicolon is the stopping condition but is not part of the match.
Backtracking and choosing the right tool
.*? is convenient but can still backtrack heavily in complicated expressions. If you know the stopping characters, a negated class such as [^;]* is often faster and clearer.
- Use a single-match API when you want only the first overall match.
- Use lazy quantifiers or delimiter-aware classes when you want the first stopping point inside one match.
The most common mistakes are writing a more complicated pattern to solve what is really an API problem, using greedy .* when you mean to stop at the first delimiter, and reaching for lazy quantifiers when a precise negated class would be simpler and more robust.
Related reading
- Relating NP-Complete problems to real world problems
- Relational Fisher Kernel Implementation
- Relationship between BFS and topological sort
- Relationship between NP-hard and undecidable problems
- Relaxation of an edge in Dijkstra's algorithm
- remove elements from link list whose sum equals to zero
- Remove item from list based on condition
- Remove nodes from graph or reset entire default graph

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.