Quick Recap # In Part 3, we covered character classes: square brackets that match one character from a set, POSIX classes like [[:digit:]], and negation with [^...]. Now let’s talk about how many characters to match. The Four Core Quantifiers # A quantifier tells the regex engine “how many of the previous thing do I want?” There are four you’ll use constantly: Quantifier Meaning Example * Zero or more ab*c matches ac, abc, abbc + One or more ab+c matches abc, abbc, but NOT ac ? Zero or one (optional) colou?r matches color and colour {n} Exactly n a{3} matches aaa A Critical Detail: What Gets Quantified # The quantifier applies to the thing directly before it. This is probably the most common beginner mistake. abc+ # Matches: ab followed by one or more c's # (abcc, abccc, etc.) # Does NOT mean "one or more abc" If you want “one or more abc,” you need parentheses: (abc)+. We’ll cover grouping properly in Part 6. Zero or More (*) # The asterisk * is the most permissive quantifier. “Zero…
No comments yet. Log in to reply on the Fediverse. Comments will appear here.