Enumerating Regular Expressions

The NSRegularExpression class offers a block-based enumeration approach to finding matches within a string. Use this approach to apply updates to given ranges. When you work with attributed text, you can apply color or font hints to just the elements that match the regex. This is similar to a text view’s spell checker, which adds underline to highlight misspelled words.

To roll your own, create a regular expression. Enumerate it over a string (typically one found in a text view of some sort) and use each range to create some kind of visual update. With attributed strings, it’s easier than ever to add visual feedback to text view contents:

// Check for matches NSRegularExpression *regex ...

Get The Core iOS Developer’s Cookbook, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.