Regular Expressions Tutorial: Getting Started

by · September 11, 2018

Update note: Tom Elliott updated this tutorial for Swift 4.2. James Frost wrote the original.

Regular Expression Basics

If you haven’t heard of regular expressions — also called regex — before, it’s probably worth wrapping your head around the basics before continuing with this tutorial. Fortunately, we have you covered! Check out this Introduction to Regular Expressions tutorial here.

Implementing Regex in iOS

Now that you know the basics, it’s time to use regular expressions in an app.

Use the Download Materials button at the top or bottom of this tutorial to download the starter project. Open the iRegex starter project in Xcode and run it.

You’re going to build a diary app for your boss — a Super Villain! Everyone knows that Super Villains need to keep track of all their diabolical plans for world domination, right? There’s lots of planning to do and you, as the minion, are part of these plans — your part being to build the app for the other plans!

The UI for the app is mostly complete, but the core functionality of the app relies on regular expressions, which it doesn’t have… yet!

Your job in this tutorial is to add the required regular expressions into this app to make it shine (and hopefully avoid being thrown into a vat of molten hot magma).

Here are a few sample screen shots demonstrating the final product:

The final app will cover two common use cases with regular expressions:

Performing text search: highlighting, as well as search and replace.

Validating user input.

You’ll start by implementing the most straightforward use of regular expressions: text search.

Implementing Search and Replace

Here’s the basic overview of the search-and-replace functionality of the app:

The Search view controller, SearchViewController has a read-only UITextView that contains an excerpt from your boss’ private diary.

The Navigation bar contains a Search button that will present SearchOptionsViewController modally.

This will allow your evil boss to type information into the field and tap “Search.”

The app will then dismiss the Search view and highlight all matches from the diary in the Text view.

If your boss selected the “Replace” option in SearchOptionsViewController, the app will perform a search-and-replace function for all matches in the text, instead of highlighting the results.

Note: Your app uses the NSAttributedString property of UITextView to highlight the search results.
You could also implement the highlighting functionality using Text Kit. Be sure to check out the Text Kit Tutorial in Swift to find out more.

There’s also a Reading Mode button that will allow highlighting all the dates, times and splitters between each entry in the diary. For simplicity’s sake, you won’t cover every possible format of date and time strings that can appear in the text. You’ll implement this highlighting functionality at the very end of the tutorial.

Your first step to getting the search functionality to work is to turn standard strings representing regular expressions into NSRegularExpression objects.

Open SearchOptionsViewController.swift. SearchViewController presents this view controller modally and allows the user to enter his or her search (and optional replace) terms, as well as specifying whether the search should be case sensitive or match only whole words.

Take a look at the SearchOptions struct at the top of the file. SearchOptions is a simple struct that encapsulates the user’s search options. The code passes an instance of SearchOptions back to SearchViewController. It would be good to be able to use this directly to construct an appropriate NSRegularExpression. You can do this by adding a custom initializer to NSRegularExpression with an extension.

Choose File ▸ New ▸ File… and choose Swift File. Name your file RegexHelpers.swift. Open the new file and add the following code:

This code adds a convenience initializer to NSRegularExpression. It uses the various settings within the passed-in SearchOptions instance to configure things correctly.

Things to note:

Whenever the user requests a case-insensitive search, the regular expression uses the .caseInsensitiveNSRegularExpressionOptions value. The default behavior of NSRegularExpression is to perform case-sensitive searches, but, in this case, you’re using the more user-friendly default of case-insensitive searches.

If the user requests a whole word search, then the app wraps the regular expression pattern in the b character class. Recall that b is the word boundary character class, so putting b before and after the search pattern will turn it into a whole word search (that is, the pattern “bcatb” will match only the word “cat,” but not “catch”).

If, for any reason, it’s not possible to create the NSRegularExpression, then the initializer will fail and return nil. Now that you have the NSRegularExpression object, you can use it for matching text.

Open SearchViewController.swift, find searchForText(_:replaceWith:inTextView:), and add the following implementation to the empty method stub:

First, this method captures the current text in the UITextView and calculates the range of the entire string. It’s possible to apply a regular expression to just a part of your text, which is why you need to specify the range. In this case, you’re using the the entire string, which will result in the regular expression being applied to all of your text.

The real magic happens in the call to stringByReplacingMatches(in:options:range:withTemplate:). This method returns a new string without mutating the old string. Then the method sets the new string on the UITextView so that the user can see the results.

Still in SearchViewController, find highlightText(_:inTextView:) and add the following:

Then, create an NSRange for the entire length of the text and remove any background color text attributes that already exist within it.

As with find and replace, create a regular expression using your convenience initializer and fetch an array of all matches for the regular expression within the textview’s text.

Loop through each match and add a yellow color background attribute for each one.

Finally, update the UITextView with the highlighted results.

Build and run your app. Try searching for various words and groups of words! You’ll see the search terms highlighted throughout your text, as shown in the image below:

Try searching for the word “the” using various options and see the effects. Notice, for example, that, when using whole words, the ‘the’ in ‘then’ does not highlight.

Also, test out the search-and-replace functionality to see that your text strings are replaced as expected. Also try both the ‘match case’ and ‘whole words’ options.

Highlighting and replacing text are both great. But how else can you effectively use regular expressions in your apps?

Validating Data

Many apps will have some kind of user input, such as a user entering an email address or phone number. You’ll want to perform some level of data validation on this user input, both to ensure data integrity and to inform the user of any mistakes entering the data.

Regular expressions are perfect for many kinds of data validation since they are excellent at pattern matching.

There are two things you need to add to your app: the validation patterns themselves and a mechanism to validate the user’s input with those patterns.

As an exercise, try to come up with the regular expressions to validate the following text strings (don’t worry about case sensitivity):

First name: Should consist of standard English letters and between 1 and 10 characters in length.

Middle initial: Should consist of one single English letter.

Last name: Should consist of standard English letters plus the apostrophe (for names such as O’Brian), the hyphen (for names such as Randell-Nash) and be between two and 20 characters in length.

Super Villain name: Should consist of Standard English letters, plus the apostrophe, the period, the hyphen, digits and spaces, as well as between two and 20 characters in length. This allows names such as Ra’s al Ghul, Two-Face and Mr. Freeze.

Password: Should have at least eight characters, including one uppercase character, one lowercase character, one number and one character that isn’t a letter or digit. This one is tricky!

Of course, you can use the iRegex playground in your materials folder to try out your expressions as you develop them.

How did you do with coming up with the required regular expressions? If you’re stuck, just go back to the cheat sheet at the top of this tutorial and look for the bits that will help you in the scenarios above.

The spoiler below shows the regular expressions you’ll use. But do try to figure them out yourself first and check your results before reading further!

This creates an array of the text fields in the view controller and an array of string patterns. It then uses Swift’s map function to create an array of NSRegularExpression objects, one for each pattern. If creating the regular expressions from the patterns fails, you fatalError in the Simulator so you can catch it quickly when developing the app, but ignore in production as you don’t want your app to crash for your users!

To create the regular expression to validate the first name, you first match from the beginning of the string. Then, you match a range of characters from A-Z before finally matching the end of the string ensuring that it is between one to 10 characters in length.

The next two patterns — middle initial and last name — follow the same logic. In case of the middle initial, you don’t need to specify the length — {1} — since ^[a-z]$ matches on one character by default. The super villain name pattern is also similar but starting to look a bit more complicated as you add in support for the special characters: apostrophe, hyphen, and period.

Note that you’re not worrying about case sensitivity, here — you’ll take care of that when instantiating the regular expression.

Now, what about that password validator? It’s important to stress that this is simply an exercise to show how regexes can be used and you really shouldn’t use this in a real-world app!

With that said, how does it actually work? First, a reminder on some regex theory:

(Parentheses) define a capture group that groups together part of your regular expression.

When a capture group starts with ?=, this denotes that the group is to be used as a positive lookahead, matching the previous pattern only if it is followed by the pattern in the capture group. For example, A(?=B) matches an A, but only if followed by a B. Lookahead is an assertion like ^ or $, and doesn’t consume any characters itself.

p{} matches Unicode characters within a certain category, and P{} matches Unicode characters not in a certain category. A category might be, for example, all letters (p{L}), all lowercase letters (p{Lu}) or numbers (p{N}).

Using these, break down the regex itself:

^ and $ match the beginning and end of a line, as is usual.

(?=P{Ll}*p{Ll}) match (but doesn’t consume) any number of non-lowercase Unicode characters followed by a lowercase Unicode character, in effect matching a string with at least one lowercase character.

(?=P{Lu}*p{Lu}) follows a similar pattern to above, but ensures at least one uppercase character.

(?=P{N}*p{N}) ensures at least one digit.

(?=[p{L}p{N}]*[^p{L}p{N}]) ensures at least one character that isn’t a letter or digit by using the carat (^) to negate a pattern.

And finally, [sS]{8,} matches any character eight or more times by matching whitespace or non-whitespace characters.

Phew! Well done!

You can get very creative with regular expressions. There are other ways to solve the above problem, such as using d instead of [0-9]. However, any solution is perfectly fine as long as it works!

Now that you have the patterns, you need to validate the entered text in each of the text fields.

Still in AccountViewController.swift, find validate(string:withRegex:) and replace the dummy implementation with the following:

This is very similar to what you did in SearchViewController.swift. Starting off in validateTextField(_:), you grab the relevant regex from the regexes array and trim any whitespace off the user’s input for the text field.

Then, in validate(string:withRegex:) you create a range for the whole text and check for a match by testing the result of rangeOfFirstMatch(in:options:range:). This is probably the most efficient way to check for a match, since this call exits early when it finds the first match. However, there are other alternatives such as numberOfMatches(in:options:range:) if you need to know the total number of matches.

Using the same validate(string:withRegex:) method from above, this method simply tests if every non-empty text field is valid.

Run the project, click on the Account icon button at the top-left, and try entering some information into the sign-up form. When you complete each field, you should see its text turn green or red depending on whether or not it’s valid, as in the screen shot below:

Try and save your account. Notice that you can only do this when all the text fields validate correctly. Relaunch the app. This time, when the app starts up, you are presented with a sign-up form before you can see the secret plans in the diary. Type in the password you just created and click Login.

Note: This is a tutorial on regular expressions, not authentication! Do not use the code in this tutorial as an example of best practices for authentication. To hammer the point home, the password is stored in plain text on the device. The loginAction in LoginViewController only checks the password as stored on the device rather than stored securely on a server. This is not secure in any way.

Handling Multiple Search Results

You haven’t used the Reading Mode button found on the Navigation bar yet. When the user taps on it, the app should enter a ‘focused’ mode, highlighting any date or time strings in the text, as well as highlighting the end of every diary entry.

Open SearchViewController.swift in Xcode, and find the following implementation for the Reading Mode button item:

The method above calls three other helper methods to decorate dates, times and diary entry splitters in the text. Each method takes a decoration option for either underlining the text or setting no decoration (removing the underlining). If you look at the implementation of each of the helper methods above, you will see they are empty!

Before you worry about implementing the decoration methods, you should define and create the NSRegularExpressions themselves. A convenient way to do this is to create static variables on NSRegularExpression. Switch to RegexHelpers.swift and add the following placeholders inside the NSRegularExpression extension:

Now, it’s your job to complete these patterns! Here are the requirements:

Date Requirements:

xx/xx/xx or xx.xx.xx or xx-xx-xx format. Day, month and year placement is not important since the code will just highlight them. Example: 10-05-12.

Full or abbreviated month name (e.g., Jan or January, Feb or February, etc.), followed by a 1 or 2 digit number (e.g., x or xx). The day of the month can be ordinal (e.g., 1st, 2nd, 10th, 21st, etc.), followed by a comma as separator, and then a four-digit number (e.g., xxxx). There can be zero or more white space between the name of the month, day and year. Example: March 13th, 2001

Time Requirements:

Find simple times like “9am” or “11 pm”: One or two digits followed by zero or more white spaces, followed by either lowercase “am” or “pm”.

Splitter Requirements:

A sequence of tilde (~) characters at least 10 long.

You can use the playground to try these out. See if you can figure out the needed regular expressions!

Here are three sample patterns you can try. Replace the empty pattern for of regularExpressionForDates with the following:

Each of these methods uses one of the static variables on NSRegularExpression to create the appropriate regular expression. They then find matches and call highlightMatches(_:) to color and underline each string in the text or removeHighlightedMatches(_:) to revert the style changes. Check out their implementations if you’re interested to see how they work.

Build and run the app. Now, tap on the Reading Mode icon. You should see the link-style highlighting for dates, times and splitters, as shown below:

Tap the button again to disable reading mode, and return the text to its normal style.

While it’s fine for this example, can you see why the regular expression for time might not be right for more general searching? As it stands, it would not match the time 3:15pm and it would match 28pm.

Here’s a challenging problem! Figure out how to rewrite the regular expression for time so that it matches a more a general time format.

Specifically, your answer should match times in the format ab:cd am/pm for a standard 12-hour clock. So it should match: 11:45 am, 10:33pm, 04:12am but not 2pm, 0:00am 18:44am 9:63pm or 7:4 am. There should be, at most, one space before the am/pm. By the way, it’s acceptable if it matches the 4:33am in 14:33am.

One possible answer appears below, but try it yourself first. Check the end of the accompanying playground to see it in action.

[spoiler]

"(1[0-2]|0?[1-9]):([0-5][0-9]\s?(am|pm))"

[/spoiler]

Where to Go From Here?

Congratulations! You now have some practical experience with using regular expressions.

You can download the completed version of the project using the Download Materials button at the top or bottom of this tutorial.

Regular expressions are powerful and fun to work with — they’re a lot like solving a math problem. The flexibility of regular expressions gives you many ways to create a pattern to fit your needs, such as filtering input strings for white spaces, stripping out HTML or XML tags before parsing, or finding particular XML or HTML tags — and much more!

One More Exercise…

There are a lot of real-world examples of strings that you can validate with regular expressions. As a final exercise, try to untangle the following regular expression that validates an email address:

It looks like a jumble of characters at first glance, but with your new-found knowledge (and the helpful links below) you’re one step closer to understanding it and becoming a master of regular expressions!

More Resources

Here is a short list of some useful resources about regular expressions:

I hope you enjoyed this NSRegularExpression tutorial, and if you have any comments or questions, please join the forum discussion below!