2025-04-10 19:39:34 EDT

M: I’m trying to do a regex search and replace. I can’t remember the terminology, but I need my wild card searches to return the minimal result that satisfies the given expression, not the maximum.

G: You're looking for non-greedy (or lazy) matching in regular expressions.

TLDR:

Use *?, +?, or ?? instead of *, +, or ? for non-greedy matching.

Examples:

Common greedy vs. non-greedy forms:

Greedy Non-Greedy Meaning
* *? 0 or more
+ +? 1 or more
? ?? 0 or 1
{n,m} {n,m}? Between n and m

Want to post your regex and target string? I can help you tweak it.