Regular Expressions
Programming
Coding
Pattern Matching
Algorithms

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:

python
1import re
2
3text = "id=12 id=34 id=56"
4match = re.search(r"id=\d+", text)
5print(match.group())

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:

python
1import re
2
3text = "axxxbxxaxxb"
4print(re.search(r"a.+b", text).group())

This matches axxxbxxaxxb because .+ is greedy and consumes as much as possible.

A lazy pattern fixes it:

python
1import re
2
3text = "axxxbxxaxxb"
4print(re.search(r"a.+?b", text).group())

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:

python
1import re
2
3text = 'say "one" and "two"'
4print(re.search(r'".+?"', text).group())
5print(re.search(r'"[^"]*"', text).group())

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

python
1import re
2
3text = "<div>Hello</div><div>World</div>"
4print(re.search(r"<div>.*?</div>", text).group())

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:

python
1import re
2
3text = "name=alice;role=admin"
4match = re.search(r"name=.*?(?=;)", text)
5print(match.group())

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.


Course
Intermediate
27 lessons
15 hours
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 course
Data 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.

Practice algorithms