Greediness
The above described qualifiers behave greedy by default. That means that they match as many characters from the source string as they can. For example, in the match 'bbb' ~~ /b*/ , all the three characters will always be consumed by the regex. Similarly, the + quantifier tries to consume as many repeated characters as possible.
Sometimes that behavior is not desired. Consider a regex for selecting attributes from HTML tags. From the given string <a href="index.html" class="menu">, we want to extract the values of the attributes, which are strings inside the quotes. An attempt to create a regex such as / \" .* \" / will take the whole substring between the first and the last quote—"index.html" class="menu". This is because the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access