![]() |
|
[Tut] Python Regex Greedy vs Non-Greedy Quantifiers - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python Regex Greedy vs Non-Greedy Quantifiers (/thread-93697.html) |
[Tut] Python Regex Greedy vs Non-Greedy Quantifiers - xSicKxBot - 02-21-2020 Python Regex Greedy vs Non-Greedy Quantifiers Ready to earn the black belt of your regex superpower? This tutorial shows you the subtle but important difference between greedy and non-greedy regex quantifiers. But first things first: what are “quantifiers” anyway? Great question – I’m glad you asked! So let’s dive into Python’s three main regex quantifiers. Python Regex Quantifiers The word “quantifier” originates from latin: it’s meaning is quantus = how much / how often. This is precisely what a regular expression quantifier means: you tell the regex engine how often you want to match a given pattern. If you think you don’t define any quantifier, you do it implicitly: no quantifier means to match the regular expression exactly once. So what are the regex quantifiers in Python? Quantifier | Meaning Code: A?Match regular expression Code: ACode: A*Match regular expression Code: ACode: A+Match regular expression Code: ACode: A{m}Match regular expression Code: ACode: A{m,n}Match regular expression Code: ANote that in this tutorial, I assume you have at least a remote idea of what regular expressions actually are. If you haven’t, no problem, check out my detailed regex tutorial on this blog. You see in the table that the quantifiers Code: ?Code: *Code: +Code: {m}Code: {m,n}Code: ALet’s have a look at some examples—one for each quantifier: Code: >>> import re
>>> re.findall('a?', 'aaaa')
['a', 'a', 'a', 'a', '']
>>> re.findall('a*', 'aaaa')
['aaaa', '']
>>> re.findall('a+', 'aaaa')
['aaaa']
>>> re.findall('a{3}', 'aaaa')
['aaa']
>>> re.findall('a{1,2}', 'aaaa')
['aa', 'aa']In each line, you try a different quantifier on the same text Code: 'aaaa'
You’ve learned the basic quantifiers of Python regular expressions. Now, it’s time to explore the meaning of the term greedy. Shall we? Python Regex Greedy Match A greedy match means that the regex engine (the one which tries to find your pattern in the string) matches as many characters as possible. For example, the regex Code: 'a+'Code: 'a'Code: 'aaaa'Code: 'a'Code: 'aa'Code: 'aaa'Code: 'a+'In other words, the greedy quantifiers give you the longest match from a given position in the string. As it turns out, all default quantifiers Code: ?Code: *Code: +Code: {m}Code: {m,n}Here are the above examples again that all show how greedy the regex engine is: Code: >>> import re
>>> re.findall('a?', 'aaaa')
['a', 'a', 'a', 'a', '']
>>> re.findall('a*', 'aaaa')
['aaaa', '']
>>> re.findall('a+', 'aaaa')
['aaaa']
>>> re.findall('a{3}', 'aaaa')
['aaa']
>>> re.findall('a{1,2}', 'aaaa')
['aa', 'aa']In all cases, a shorter match would also be valid. But as the regex engine is greedy per default, those are not enough for the regex engine. Okay, so how can we do a non-greedy match? Python Regex Non-Greedy Match A non-greedy match means that the regex engine matches as few characters as possible—so that it still can match the pattern in the given string. For example, the regex Code: 'a+?'Code: 'a'Code: 'aaaa'Code: 'a'In other words, the non-greedy quantifiers give you the shortest possible match from a given position in the string. You can make the default quantifiers Code: ?Code: *Code: +Code: {m}Code: {m,n}Code: '?'Code: ??Code: *?Code: +?Code: {m,n}?Here are some examples that show how non-greedy matching works: Non-Greedy Question Mark Operator (??) Let’s start with the question mark (zero-or-one operator): Code: >>> import re
>>> re.findall('a?', 'aaaa')
['a', 'a', 'a', 'a', '']
>>> re.findall('a??', 'aaaa')
['', 'a', '', 'a', '', 'a', '', 'a', '']In the first instance, you use the zero-or-one regex Code: 'a?'Code: 'a'In the second instance, you use the non-greedy zero-or-one version Code: 'a??'Code: 'a'Code: 'a'Code: 'a'Non-Greedy Asterisk Operator (*?) Let’s start with the asterisk (zero-or-more operator): Code: >>> re.findall('a*', 'aaaa')
['aaaa', '']
>>> re.findall('a*?', 'aaaa')
['', 'a', '', 'a', '', 'a', '', 'a', '']First, you use the zero-or-more asterisk regex Code: 'a*'Code: 'a'Second, you use the non-greedy zero-or-one version Code: 'a*?'Code: 'a'Non-Greedy Plus Operator (+?) Let’s start with the plus (one-or-more operator): Code: >>> re.findall('a+', 'aaaa')
['aaaa']
>>> re.findall('a+?', 'aaaa')
['a', 'a', 'a', 'a']First, you use the one-or-more plus regex Code: 'a+'Code: 'a'Second, you use the non-greedy one-or-more version Code: 'a+?'Code: 'a'Let’s summarize what you’ve learned so far: Greedy vs Non-Greedy Match – What’s the Difference? Given a pattern with a quantifier (e.g. the asterisk operator) that allows the regex engine to match the pattern multiple times. A given string may match the regex in multiple ways. For example, both substrings Code: 'a'Code: 'aaa'Code: 'a*'Code: 'aaaa'So the difference between the greedy and the non-greedy match is the following: The greedy match will try to match as many repetitions of the quantified pattern as possible. The non-greedy match will try to match as few repetitions of the quantified pattern as possible. Examples Greedy vs Non-Greedy Match Let’s consider a range of examples that help you understand the difference between greedy and non-greedy matches in Python: Code: >>> import re
>>> re.findall('a+', 'aaaa')
['aaaa']
>>> re.findall('a+?', 'aaaa')
['a', 'a', 'a', 'a']
>>> re.findall('a*', 'aaaa')
['aaaa', '']
>>> re.findall('a*?', 'aaaa')
['', 'a', '', 'a', '', 'a', '', 'a', '']
>>> re.findall('a?', 'aaaa')
['a', 'a', 'a', 'a', '']
>>> re.findall('a??', 'aaaa')
['', 'a', '', 'a', '', 'a', '', 'a', '']Make sure you completely understand those examples before you move on. If you don’t, please read the previous paragraphs again. Which is Faster: Greedy vs Non-Greedy? Considering that greedy quantifiers match a maximal and non-greedy a minimal number of patterns, is there any performance difference? Great question! Indeed, some benchmarks suggest that there’s a significant performance difference: the greedy quantifier is 100% slower in realistic experiments on benchmark data. So if you optimize for speed and you don’t care about greedy or non-greedy matches—and you don’t know anything else—go for the non-greedy quantifier! However, the truth is not as simple. For example, consider the following basic experiment that falsifies the previous hypothesis that the non-greedy version is faster: Code: >>> import timeit
>>> timeit.timeit('import re;re.findall("a*", "aaaaaaaaaaaa")')
1.0579840000000331
>>> timeit.timeit('import re;re.findall("a*?", "aaaaaaaaaaaa")')
3.7830938000000742I used the speed testing tool timeitthat allows to throw in some simple Python statements and check how long they run. Per default, the passed statement is executed 1,000,000 times. You can see a notable performance difference of more than 300%! The non-greedy version is three times slower than the greedy version. Why is that? The reason is the re.findall() method that returns a list of matching substrings. Here’s the output both statements would produce: Code: >>> re.findall("a*", "aaaaaaaaaaaa")
['aaaaaaaaaaaa', '']
>>> re.findall("a*?", "aaaaaaaaaaaa")
['', 'a', '', 'a', '', 'a', '', 'a', '', 'a', '', 'a', '', 'a', '', 'a', '', 'a', '', 'a', '', 'a', '', 'a', '']You can see that the greedy version finds one match and is done with it. The non-greedy version finds 25 matches which leads to far more processing and memory overhead. So what happens if you use the re.search() method that returns only the first match rather than the re.findall() method that returns all matches? Code: >>> timeit.timeit('import re;re.search("a*", "aaaaaaaaaaaa")')
0.8420328999998219
>>> timeit.timeit('import re;re.search("a*?", "aaaaaaaaaaaa")')
0.7955709000000297As expected, this changes things again. Both regex searches yield a single result, but the non-greedy match is much shorter: it matches the empty string Code: ''Code: 'aaaaaaaaaaaa'However, the difference is negligible in this minimal example. There’s More: Greedy, Docile, Lazy, Helpful, Possessive Match In this article, I’ve classified the regex world into greedy and non-greedy quantifiers. But you can differentiate the “non-greedy” class even more! Next, I’ll give you a short overview based on this great article of the most important terms in this regard:
If you want to learn more about those, I’d recommend that you read this excellent online tutorial. Where to Go From Here Summary: You’ve learned that the greedy quantifiers Code: ?Code: *Code: +Code: ??Code: *?Code: +?If you want to master Python and regular expressions, join my free email academy—it’s fun! |