February 2019
Intermediate to advanced
626 pages
15h 51m
English
Each of these different quantifiers is greedy. A greedy quantifier will grab as much as it possibly can before allowing the regex engine to move on to the next character in the expression.
In the following example, the expression has been instructed to match everything it can, ending with a \ character. As a result, it takes everything up to the last \, because the expression is greedy:
PS> 'C:\long\path\to\some\files' -match '.*\\'; $matches[0]TrueC:\long\path\to\some\
The repetition operators can be made lazy by adding the ? character. A lazy expression, by contrast, will get as little as it can before it ends:
PS> 'C:\long\path\to\some\files' -match '.*?\\'; $matches[0]TrueC:\
A possible use of a lazy quantifier ...
Read now
Unlock full access