Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In the previous movie, we began formatting A Midsummer Night's Dream to match the changes that the director of the play wanted to the formatting, and we did it using regular expressions. We have really just got our feet wet, and saw how regular expressions can really help speed up these kinds of tasks. Now the director has asked us to do something a little more advanced. The director comes to us and says that really she would like this block here -- the Act I, Scene I location block -- to be modified, and to look a little bit different. So we say great; can you tell us what you would want it to look like? She says that she'd like it look something like this instead.
Here it is, so you can compare the two side by side. We have 10 dashes on one line, then we have Act, colon, and the same Roman numeral, Scene, colon, and the same Roman numeral, and the location now has dropped down to a new line, and has a location, colon in front of it, and then a line with 10 dashes at the end. So in order to write a regular expression for this, the way that I typically do it is that I would copy my target text -- what I'm trying to find -- and I would open up my regular expression area here. I can actually make it bigger by clicking this, and then I would paste in the actual text.
Now, that's not really a regular expression; that's just going to match the actual text, word for word, but that's a good starting place. And same thing for the ending text; what I want to replace it with. Let's go ahead and just replace it with that exact text as well. I am just checking to see that they both have a line return at the end, so that those will both line up. Now, I could do a Find & Replace, and it would find one of them, and replace it, but I want to now write a regular expression that will generalize each of these. So what's common about these? Well, there always going to have Act, space, and then a Roman numeral after it.
So that Roman numeral, though, is something that we want to preserve going down to here. Well we know how to do that; we did that with captures. So we want to capture that Roman numeral. We could put in all the characters that could possibly be Roman numerals here. There's probably not that many that are being used, because I am pretty sure there's not more than 10 Acts, but I'm going to go ahead and just make it very general, and say anything A to Z, repeated, we'll make it not greedy, and then close our capture at the end. Now, if we capture it here, then we need to reuse it down here. We've seen before that we can do backreferences using backslash, 1, but actually, when we were doing Find & Replace, at least in TextMate, it only uses that backreference when it's in the same regular expression.
Once it's out of the regular expression, and we are doing the replace, it's a dollar sign. So dollar sign, 1 will now refer to that first capture. Okay, we can do the same thing, let's take that same capture, and let's drop it down here into Scene I, and we can do the same thing, because those are also Roman numerals, and now we know that this is going to be capture number 2. And this here that I have a period that was in my literal text; if this is going to be a regular expression, I need to make sure I escape that, so that it's still a literal period. And then for all of this of this block here, that's going to be the location information.
So, of course, I want to capture that as well. So let's put in our parentheses, and let's capture it, and what can be in there? Well, we will say that it can actually be anything; it has to be at least one character. We will also make it not greedy, just for good measure. And then all of this can be changed into dollar sign, 3. Oops! And I lost my line return. Let me just paste in a line return there at the end. Okay, now just to make this a good proper regular expression, I am also just going to take out these line returns, and make them into backslash, n. So it still should find the same thing, but now I have got one concise, well written regular expression.
So let's see if we can find things first. Let's jump up to the top, and let's do a Find, and do Next. Okay, it found it. Let's do the next one. Act I, Act II; there it is. It's finding all of the Acts, and the different Scenes. So it's working. Now let's try matching. Let's jump up to the top again. Let's find it, and let's do Replace & Find. Let's go back and review. Jump to the top, Act I, Scene I; there it is. It looks exactly like we wanted. Let's try it on the second one, and make sure that our generalization worked.
Let's do a Replace & Find. I am going to go ahead just do it for all of them, and there it is. It worked here for the last one as well. So we were able to make the second change that our director wanted, and we did it by using captures, and then references to those captures, in our replacement string. In the next movie, we will take a look at the third and final task.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101160 Viewers
61 Video lessons · 87919 Viewers
71 Video lessons · 71798 Viewers
56 Video lessons · 103662 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.