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.
Use *?, +?, or ?? instead of
*, +, or ? for non-greedy
matching.
".*" matches the longest
string between quotes.".*?" matches the shortest
string between quotes.| 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.