Regex Cheatsheet
Free regex cheatsheet. Searchable reference for regular expression syntax โ anchors, character classes, quantifiers, groups, lookaround, and flags.
Frequently asked questions
What is the difference between * and +?
* matches zero or more of the preceding element, so it still matches if the element isn't present at all. + requires at least one occurrence.
What is the difference between a capturing and non-capturing group?
A capturing group (abc) remembers the text it matches, so you can refer back to it (as a backreference, or in replacement text). A non-capturing group (?:abc) groups a sequence โ for applying a quantifier to the whole thing, for example โ without the overhead of remembering what it matched.
What is lookahead/lookbehind, and when would I use it?
Lookaround asserts that something comes before or after a position, without including it in the match itself. It's useful when you need to match something only in a certain context โ e.g. (?<=\$)\d+ matches digits only when preceded by a dollar sign, without the dollar sign itself becoming part of the match.
Why is my pattern matching more than I expect?
Quantifiers are greedy by default โ .* grabs as much as it possibly can while still letting the rest of the pattern match, which often matches far more than intended across multiple expected "stopping points" in a string. Add a ? after the quantifier (.*?) to make it lazy, matching as little as possible instead.