RegEx: Smallest possible match or nongreedy match

.NetRegexRegex GreedyNon Greedy

.Net Problem Overview


How do I tell RegEx (.NET version) to get the smallest valid match instead of the largest?

.Net Solutions


Solution 1 - .Net

For a regular expression like .* or .+, append a question mark (.*? or .+?) to match as few characters as possible. To optionally match a section (?:blah)? but without matching unless absolutely necessary, use something like (?:blah){0,1}?. For a repeating match (either using {n,} or {n,m} syntax) append a question mark to try to match as few as possible (e.g. {3,}? or {5,7}?).

The documentation on regular expression quantifiers may also be helpful.

Solution 2 - .Net

The non-greedy operator, ?. Like so:

.*?

Solution 3 - .Net

The non greedy operator does not mean the shortest possible match:

> abcabk

a.+?k will match the entire string (in this example) instead of only the last three signs.

I'd like to actually find the smallest possible match instead.

That is that last possible match for 'a' to still allow all matches for k.

I guess the only way to do that is to make use of an expression like:

a[^a]+?k

const haystack = 'abcabkbk';
const paternNonGreedy = /a.+?k/;
const paternShortest = /a[^a]+?k/;

const matchesNonGreedy = haystack.match(paternNonGreedy);
const matchesShortest = haystack.match(paternShortest);

console.log('non greedy: ',matchesNonGreedy[0]);
console.log('shortest: ', matchesShortest[0]);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonathan AllenView Question on Stackoverflow
Solution 1 - .NetDMIView Answer on Stackoverflow
Solution 2 - .NetDavid HedlundView Answer on Stackoverflow
Solution 3 - .NetJonathanView Answer on Stackoverflow