NSRegularExpression Cheat Sheet and Quick Reference

NSRegularExpression Cheat Sheet and Quick Reference

Special Characters

*?+[(){}^$|\./

Operators

Pattern Description
\ Or
* 0 or more times. Match as many times as possible.
+ 1 or more times. Match as many times as possible.
? 0 or 1 times. Prefer 1.
*? 0 or more times. Match as few times as possible.
+? 1 or more times. Match as few times as possible.
?? 0 or 1 times. Prefer 0.
*+ 0 or more times. Match as many times as possible when first encountered, do not retry with fewer even if overall match fails (Possessive Match).
++ 1 or more times. Possessive match.
?+ 0 or 1 times. Possessive match.
{n} {n}? {n}+ Exactly n times.
{n,} n or more.
{n,}? At least n times, but no more than required for an overall pattern match.
{n,m} Between n and m times.
{n,m}? Between n and m times. Match as few times as possible, but not less than n.

Anchors

Pattern Description
^ Beginning of a line.
$ End of a line.
\A Beginning of an input. Doesn’t match after a new line within the input.
\z End of input.
\Z End of input, but before the final line terminator, if one exists.
. Any character.
\ Quote (escape) following character.

Character Classes

Pattern Description
\b Word boundary, if outside of a [Set]. BACKSPACE, if within a [Set].
 \B Not word boundary.
 \s White space character.
\S Non-white space character.
\d Digit character.
\D Non-digit character.
\w Word character.
\W Non-word character.

Groups and Ranges

Pattern Description
(…) Capturing parentheses (capturing group).
(?:…) Non-capturing parentheses. Matches but doesn’t capture. Somewhat more efficient than capturing parentheses.
(?!…) Negative look-ahead. True if the parenthesized pattern does not match at the current input position.
[…] Any one character in the set.
[^…] Negated set. Not any one in the set.