5.1 Regular Expressions in Sublime
In this video, I'll provide you with the basics of using regular expressions in Sublime Text.
1.Introduction1 lesson, 02:05
2.Getting Started9 lessons, 35:20
3.Snippets3 lessons, 14:40
4.Essential Plugins12 lessons, 46:58
5.Tips, Techniques and Modifications8 lessons, 49:09
6.Closing1 lesson, 00:49
5.1 Regular Expressions in Sublime
In this video, I will demonstrate how to use regular expressions in Sublime Text. And we will also take a look at some of the various ways that we can modify a string. Specifically, the capitalization of that string. So what I have here is an actual article that we would post on Nettuts Plus. So you can see it's just standard HTML. They follow a basic guide that we have, such as they need to use heading two tags and they need to capitalize the first letter of each important word. So in this case, this was done correctly. But as you can imagine, lots of article submissions we receive, don't follow that format. So let's look for another case. I'm going to use incremental search with Cmd+I. Look for the next occurrence of an H2 and what I'm doing here is resetting it or making some of these wrong. And let's change this one and this one right here. All right. So how would you go about fixing all of these? Well, you have a couple choices. One, you could manually filter through each heading two and just update them one at a time. But with Sublime Text if you're somewhat familiar with regular expressions, we can rapidly expedite this process. I'll give you an example. Before we jump into regular expressions, if I select this text, Sublime Text offers a function that will capitalize the first letter of each word. If I go to Edit > Convert Case > Title Case. Now you can see the first letter of every word has been capitalized. And you can also access this through the command palette. So I can say Shift+Cmd+P >Title and there it is. So now we're going to run title case. So that is the first step, but how could we apply this to every occurrence of the heading twos. Well, we could use multiple cursors. So I could select the first occurrence, then hold down command and select the second and now you can see we have multiple cursors there. And then I can run title case and that updates it there. But it also updates it at the top, but that's still a little bit too strenuous. I would have to manually go through every heading two and update that. Now if you happen to know regular expressions, we can improve this. Here's what I'll do. I'll hit Cmd+I to do incremental search and I'm gonna look for a heading two tag and then any character and then a closing heading two tag. Now what you'll see here is that it's not selecting this first h2 and that's because we have to turn on the regex feature. And we can do this right down here at this bottom-left icon, regular expressions. Or you can press Option+Cmd+R and I'll do that right now. Now you can see it is selecting that. So the most important thing to remember, even if you don't know regular expressions, is that a period represents any character and a plus will represent one or more of the preceding character. So all we're doing here is looking for an h2 tag, then characters in between and then a closing h2 tag. So it did select this first occurrence, but if I scroll down just a ways, did it select the next occurrence? No, we haven't yet done that. But if I hit Option+Enter, that will select every occurrence. So now if I zoom out just a few clicks, so you can see. As I type, we now have multiple cursors in each location. Very cool. Now at this point, you might think we're done. We could just run Title Case and be finished, but that's not exactly going to work for us. Notice that the first letter right here was not capitalized and that's because it's right next to the closing tag there. If I hit Cmd+C to go back a step into my other space there and I'll just use regular selection here to show you. Now if I run Title Case, that will work. So what we really need to do is notice how it also capitalized the H with an h2 tag. This isn't going to work for our needs. So we need to revise our regular expression. This time, I'm going to look for an h2 and the same thing as we did before. But now, I'm going to tell it, I want to make sure that there is an h2 there, but I'm not interested in matching it. And with regular expressions, I can use this little sequence right here. So this is a negative matcher. Notice that no longer is it selecting it, but it is ensuring that that sequence of characters does occur. And then I'll do the same thing for a forward positive matcher. So I won't go over this too much. This isn't a regular expressions course. If you would like to learn more about that, we do have a course on regular expressions on Tuts Plus Premium and I recorded it. So I highly recommend you take a look. So now at this point, we've matched all the text between all heading two tags on the page. If I hit Option > Enter. Now I can switch it and notice that we're updating all of the text, in between the h 2. If I scroll down to the bottom, you'll see even down here. It's doing the exact same thing. We'll hit Cmd+C to go back a few clicks and now what we wanna do is use that title case feature. And now we've correctly capitalized the first letter of each word within all heading two tags on the page. Now if you happen to need something different, Edit > Convert Case. Maybe you want everything to be upper case. In that case, on the Mac, you can hit Cmd+K+U for that effect and Cmd+K+L, think L for lower. That will make everything lower case. So the important take away from this lesson is regular expressions. Anywhere you can do a search and replace, you can use them just as long as you have it turned on. So if I hit Cmd+Option+F to do a find and replace, I need to make sure that selection is turned on right there in the bottom-left. And additionally, as we did in this lesson, if I hit Cmd+I to do incremental search. Once again, I can use regular expressions, as well. All right. That does it for this video. I'll see you in the next one.