What is a Regular Expression?5:54 with Alena Holligan
A Regular Expression lets you match patterns in strings. We'll start with a simple example, using the Chrome browser's find functionality.
[MUSIC] 0:00 Hi, I'm Alena, a developer and teacher at Treehouse. 0:09 Welcome to your new superpower. 0:13 Regular expressions are used everywhere. 0:16 Not only in every programming language you'll ever use, but 0:18 also in most computer applications, text editors, 0:22 word processors, spreadsheets and even graphics editors. 0:26 Regular expressions are like having a secret code for 0:31 telling a computer exactly what you want. 0:34 Regex, as it's often referred to, is a text string for 0:38 describing a search pattern. 0:42 You can think of it as a supercharged version of a fine tool. 0:45 In this course, I'll show you what regular expressions are, and 0:49 we'll get some practice using them. 0:54 At the end, I'll provide resources for language specific implementations. 0:56 Let's start by looking at a simple example using a browser's find feature. 1:01 I'm on the Wikipedia page for regular expressions. 1:07 It says here, that Stephen Cole Kleene, 1:11 formalized a description of a regular language. 1:13 Let's find his name on the page. 1:17 I'll hit Cmd+F to open Chrome's find bar. 1:19 As I type Kleene's last name, 1:23 notice that chrome highlights each place the word appears. 1:26 These highlighted strings are called matches because they match the string 1:30 I'm typing in. 1:35 The matches here are case insensitive. 1:37 In other words, even though I didn't type a capital K, 1:40 Chrome still matches the word Kleene. 1:44 If I then type a space, notice that this Kleene is no longer 1:47 highlighted because it no longer matches the search that I'm looking for. 1:52 If I backspace and type an apostrophe, this one is now selected, 1:58 but the one up here is no longer selected. 2:03 What happens if I want to select both of these at once? 2:06 You can do that using a special character in a regular expression. 2:10 Because Chrome's simple search bar doesn't recognize regular expressions, 2:14 let's use an online editor called Regex101. 2:18 Regex is a common way to shorten the term, regular expressions. 2:24 On this page, we can test regex and interactively learn how they work. 2:28 You can see some extra options on the left here. 2:34 We're not gonna be using any of them, so I can zoom in a bit. 2:36 The pane on the right will be extra helpful, 2:41 as you step through your own regular expressions. 2:44 So before I zoom in too far, let me show you how these work. 2:46 Type the text you want to match in the lower window. 2:51 This is kind of like the Wikipedia page. 2:54 I'll type Kleene's name. 2:57 In the upper text field, I'll also type Kleene. 2:59 The top's text field is like the find bar of the browser. 3:05 In the bottom field, Kleene highlights, it's a match. 3:09 The explanation pane on the top shows us that our regular 3:13 expression matches the characters literally and is case-sensitive. 3:19 This is a string match, a kind of regex that only matches exact duplicates. 3:25 Next, you'll notice the global pattern flag. 3:31 If we look at the left to see the regular expression, 3:34 we see that it ends with a g and an m. 3:37 For all our work in this course, we're going to leave these default flags set. 3:40 The first flag is a g, which stands for global. 3:45 This configures the interpreter to return all matches, 3:51 not stop after the first match. 3:55 The second flag is an m, which stands for multi line. 3:57 This configures the interpreter to treat each new line as a separate test string, 4:03 as opposed to one big block of text. 4:08 After that, you'll see a pane with the details about the match. 4:11 And finally, a quick reference guide below. 4:16 I'm going to zoom in a little more, so that it's easier for 4:21 you to follow what I'm doing. 4:24 But remember, those panes are there to help you while you're building your 4:26 own regular expressions. 4:29 You type the regular expression into the top field, and 4:31 you type any text you want to search in the bottom field. 4:35 In the bottom field, I'll type Kleene again. 4:39 And you see that both instances are matched. 4:43 Let's replicate the search we did earlier on the Wikipedia page. 4:46 I'll put a space after one and an apostrophe after the other. 4:50 Now when I type a space in the upper field, we see a similar behavior. 4:56 Only one of them is matched. 5:01 When I type an apostrophe instead, the other is matched. 5:03 So far, I've just been putting strings into the top field, but 5:08 we can use other special characters to create a more flexible regular expression. 5:13 For example, I'll add a dot or period to the end of this, and 5:19 you can see that both strings are matched. 5:24 A dot has a special meaning in a regular expression. 5:26 It matches any single character, so 5:30 it will match both the space and the apostrophe. 5:33 I discuss this and other special regex characters in more detail later, so 5:37 don't worry too much about it for now. 5:41 I just wanted to give you a glimpse of what's possible. 5:44 In the next video, I'll teach you more about special regex characters and 5:47 how you can use them to find patterns in text. 5:51
You need to sign up for Treehouse in order to download course files.Sign up