Three real patterns combining everything
You now have quantifiers, character classes, anchors, and groups. Here's how they combine in real code.
HTML/XML tag (loose)
To extract simple tags like <div>, <p class="foo">: <[^>]+>. The negated class avoids the greedy .* trap. (Real HTML parsing still needs a parser — this works for ad-hoc extraction.)
Quoted string
Match anything between double quotes: "[^"]*". The negated class makes it ReDoS-safe and stops at the first closing quote. To handle escaped quotes inside (\"), the pattern grows: "(?:[^"\\]|\\.)*" — "non-quote-non-backslash, OR an escaped anything."
Number (integer or decimal, signed or unsigned)
-?\d+(\.\d+)? matches: optional minus, one or more digits, optional decimal portion. The decimal portion is wrapped in a group with ? to make the entire .\d+ sequence optional, not just the digits.
The quantifier checklist
When writing a real pattern with quantifiers, ask:
- Is each piece
+(required) or*(optional)? - Are my
.*uses justified, or should they be[^X]*? - Are my
{n,m}bounds tight (no whitespace inside)? - Did I anchor where appropriate (
\bor^...$)? - Did I test against empty input and very long input?
Five questions. They catch 90% of quantifier bugs.