Difference between greedy and non-greedy matching in RegEx
Regular expressions, also known as “RegEx” or “regex”, are a powerful tool used to match patterns in strings of text. The difference between greedy and non-greedy matching can have a big impact on the outcome of a regular expression search. In some cases, greedy matching may return unexpected results, while non-greedy matching may return the results you were looking for. In this article, we will explain the concepts behind greedy and non-greedy matching and provide examples of when and how to use them in your regular expressions.
By the end of this article, you will have a solid understanding of the difference between greedy and non-greedy matching in regular expressions and be able to use them effectively in your own projects.
This is just one out of many articles about IT. We break down complex topics into small and digestible content for you. Feel free to follow or support pandaquests for more great content about JavaScript, web development, and software development. We try to publish multiple times a week. Make sure not to miss any of our great content.
Greedy matching is the default behavior of regular expressions, where the regular expression engine will try to match as much text as possible. In contrast, non-greedy matching, also known as lazy matching, tries to match as little text as possible. For example, the regular expression .* is greedy and will match the entire input string, while the regular expression .*? is non-greedy and will only match the first character of the input string.
To illustrate the difference, here is an example of using greedy and non-greedy matching in JavaScript:
const input = "The quick brown fox jumps over the lazy dog";
// Greedy matching
const greedyRegex = /.*brown.*/;
console.log(input.match(greedyRegex));
// Output: ["The quick brown fox jumps over the lazy dog"]
// Non-greedy matching
const nonGreedyRegex = /.*?brown.*?/;
console.log(input.match(nonGreedyRegex));
// Output: ["The quick brown"]In this example, the greedy regular expression .*brown.* matches the entire input string because the .* at the beginning and end of the pattern match as much as possible. On the other hand, adding a ? after the * (zero or more) it will become anon-greedy regular expression: .*?brown.*? . This only matches the first occurrence of "brown" because the .*? at the beginning and end of the pattern match as little as possible.
Greedy matching can return unexpected results when a regular expression contains repeating or optional elements. For example, when a regular expression contains a * or + quantifier, greedy matching will match as much text as possible, while non-greedy matching will match as little text as possible.
Here’s an example of when greedy matching might return unexpected results:
const text = "The 'quick' brown fox jumps over the 'lazy' dog";
// Greedy matching:
const greedyRegex = /'.*'/g;
console.log(text.match(greedyRegex)); // Returns: ["'quick' brown fox jumps over the 'lazy'"]
// Non-greedy matching:
const nonGreedyRegex = /'.*?'/g;
console.log(text.match(nonGreedyRegex)); // Returns: ["'quick', 'lazy'"]In this example, the text contains the sentence “The ‘quick’ brown fox jumps over the ‘lazy’ dog”, and we use the regular expression /'.*'/g to match the text. We would expect to receive the words with the single quotatoin marks. Using greedy matching, the regular expression engine matches as much text as possible starting with ’ (single quotation mark) and ending with ’ (single quotation mark) which returns "’quick’ brown fox jumps over the ‘lazy’" as the match. However, using non-greedy matching, the regular expression engine matches as little text as possible starting with ’ (single quotation mark) and ending with ’ (single quotation mark) which returns ‘quick’ and ‘lazy’ as the matches.
In these examples, you can see that non-greedy matching returned exactly the text you were looking for, while greedy matching returned more than expected.
It’s good to remember that it’s not always necessary to use non-greedy matching, sometimes greedy matching returns the expected results, but in situations where you need to have more control over the match, non-greedy matching can be a good option.
In general, non-greedy matching can be faster than greedy matching, because non-greedy matching stops as soon as it finds the first match, while greedy matching continues searching for additional matches until it reaches the end of the string.
The performance difference between greedy and non-greedy matching can depend on the size and complexity of the input string, as well as the specific regular expression being used. For small or simple input strings, the performance difference may be negligible. However, for large or complex input strings, non-greedy matching can be significantly faster than greedy matching, because non-greedy matching stops searching as soon as the first match is found. The performance of a regular expression engine is also affected by other factors such as the complexity of the regular expression, the size of the input string and how optimized the engine is.
In general, it’s good practice to use non-greedy matching when you expect the match to be found early in the input string, and use greedy matching when you expect the match to be found at the end of the input string.
It’s also good practice to test your regular expressions with different inputs and different engines, in order to choose the one that performs better for your specific case.

There you have it. We hope you enjoyed this article. Do you have any questions? Let us know and comment below.
We are publishing multiple articles per week. In order not to miss any of them, follow and subscribe to pandaquests. If you want to support us directly, you can either tip or apply for becoming member with this link. By using that link, 50% of your fee will go directly to us. Only with your generous support we can retain the frequent and high quality of our articles. Thanks in advance and happy coding!






