An Introduction to Regular Expressions

This introduction to regular expressions teaches you the basics of regular expressions and how to use them. By Tom Elliott.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 2 of 2 of this article. Click here to view the first page.

NSRegularExpressions Cheat Sheet

Regular expressions are a great example of a simple syntax that can end up with some very complicated arrangements! Even the best regular expression wranglers keep a cheat sheet handy for those odd corner cases.

The official raywenderlich.com Regular Expressions Cheat Sheet PDF is included in the download materials available via the Download Materials button at the top or bottom of this tutorial.

In addition, here’s an abbreviated form of the cheat sheet below with some additional explanations to get you started:

  • . matches any character. p.p matches pop, pup, pmp, p@p, and so on.
  • \w matches any “word-like” character which includes the set of numbers, letters, and underscore, but does not match punctuation or other symbols. hello\w will match “hello_” and “hello9” and “helloo” but not “hello!”
  • \d matches a numeric digit, which in most cases means [0-9]. \d\d?:\d\d will match strings in time format, such as “9:30” and “12:45”.
  • \b matches word boundary characters such as spaces and punctuation. to\b will match the “to” in “to the moon” and “to!”, but it will not match “tomorrow”. \b is handy for “whole word” type matching.
  • \s matches whitespace characters such as spaces, tabs, and newlines. hello\s will match “hello ” in “Well, hello there!”.
  • ^ matches at the beginning of a line. Note that this particular ^ is different from ^ inside of the square brackets! For example, ^Hello will match against the string “Hello there”, but not “He said Hello”.
  • $ matches at the end of a line. For example, the end$ will match against “It was the end” but not “the end was near”
  • * matches the previous element 0 or more times. 12*3 will match 13, 123, 1223, 122223, and 1222222223
  • + matches the previous element 1 or more times. 12+3 will match 123, 1223, 122223, 1222222223, but not 13.
  • Curly braces {} contain the minimum and maximum number of matches. For example, 10{1,2}1 will match both “101” and “1001” but not “10001” as the minimum number of matches is one and the maximum number of matches is two. He[Ll]{2,}o will match “HeLLo” and “HellLLLllo” and any such silly variation of “hello” with lots of L’s, since the minimum number of matches is 2 but the maximum number of matches is not set — and therefore unlimited!

That’s enough to get you started!

It’s time to start experimenting with these examples yourself, as they’re all included in the playground mentioned above.

Where to Go From Here?

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

Make sure to download the Regular Expressions Cheat Sheet PDF and practice playground by using the Download Materials button at the top or bottom of this tutorial.

Head over to our NSRegularExpression Tutorial to learn how to use regular expressions in your Swift code! :]