Ad space (banner)
๐ŸŽObjective-C Lessons
Lesson 32 / 68

An introduction to regular expressions (pattern matching)

This lesson covers the basics of pattern matching with regular expressions in Objective-C, with the aim of checking whether a string fits a fixed format.

A regular expression is a special notation describing the shape of a string. You specify a pattern such as "three digits, a hyphen, four digits" and then test whether a string matches it or pull the matching part out. You use the NSRegularExpression class.

The sample code compiles a phone-number-shaped pattern with [NSRegularExpression regularExpressionWithPattern:...] and finds a match with firstMatchInString:. Note that backslashes have to be double-escaped inside the pattern string.

A common early stumble is remembering what the symbols mean. It looks like nothing but punctuation at first, but it is a powerful tool for tasks such as validating an email address.

In professional work, regular expressions are valued as a broadly transferable skill — learn them once and they work across most programming languages.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Objective-C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)