Viewers: in countries Watching now:
Learn how to find and manipulate text quickly and easily using regular expressions. Author Kevin Skoglund covers the basic syntax of regular expressions, shows how to create flexible matching patterns, and demonstrates how the regular expression engine parses text to find matches. The course also covers referring back to previous matches with backreferences and creating complex matching patterns with lookaround assertions, and explores the most common applications of regular expressions.
In this movie, we'll write regular expressions to match decimal numbers, and currency. We'll start with decimal numbers, which are also referred to as floating point numbers. And typically a decimal, or floating point number, looks something like this: 5.1. On the left side, we have the integer, or whole portion. In the middle, we have the dot, or decimal, and then on the right side, we have the fractional portion. Now of course, it's not just one number on each side. You can have many numbers on each side, and if there is no whole number or integer portion, then you can have a zero there, like 0.123, or you can omit the zero, and just have .345.
The zero in the second case is just assumed. There's also the possibility of the reverse of that, which is that we have an integer portion, but no fractional portion, in which case we could omit both the decimal, and the fractional portion after it, and it's just assumed. So then we could have a number like 23. That's a valid decimal number as well. So we want to make sure we allow for all these cases. Let's try it out. It may seem easy, but there is a major pitfall that I want to point out to you. So I have the same sample data for us to work with. We're going to be working with multiple lines here, so I'm going to use multi-line anchors, and then I'm going to use those anchors to make sure I match a full line, and only a full line.
Let's start just writing a basic one, and we can improve it over time. So we know that we're going to have a digit, so backslash, D, one or more times, followed by a decimal, and we want to make sure that we don't just use a dot; we use the backslash, dot to get the literal decimal, not a wildcard. Backslash, D, and a plus sign for one or more times, for the ending. So just like that, with our rough draft, we've matched the first three. So what about the next one? What are we missing? Why is it not matching? Well, it's because we've said that this digit has to occur one or more times, and it's possible that it occurs zero times.
well we have a repetition operator that does that. It's the star, or asterisk. So now it matches the fourth one, because we've made that D optional. So what about the last possibility; the fifth one? Well, that's the same thing, but in reverse. Now not only is the digit optional, but the decimal is optional as well. So let's put a question mark there, and now it's optional, and we've matched all five of them. However, this is the big pitfall: we've accidentally made a big mistake in our regular expression. What would happen if we just had a new line? Let's just put a return here.
Would it match on that empty string for that new line? The answer is yes, it would, because all three of those portions are now optional. Notice that; there's a possibility that the first digit doesn't exist, that the decimal doesn't exist, and the last digit doesn't exist. Therefore, it matches on an empty zero-width string. This is actually easier to see if we jump over to TextMate real quick. I've got the same set here. I'll do a Find with the same expression right here. I've got Regular expression marked, and let's just go to the top here.
Here we are, and now I'm going to use Command+G, just to step through each of the lines. So we've matched the first line, it matches the second line, it matches the third line, and look here: it actually matches on the fourth line as well, and then it matches the fifth one, and the sixth one. So it's matching on that empty zero-width string, and we don't want that. That's a really bad side effect. So how do we solve this problem then? We do have the possibility that those things are optional. Well, I think the best thing is to divide it into two cases. There's the case in which the decimal exists, and the case in which it doesn't exist.
So let's take these back out. If the decimal does exist, then we do need to have some digits after it. So now we've allowed for everything, but the last case. Now let's use an alternation, and then in the second possibility, it's just that we have just digits by itself, one or more times. Then let's go ahead and put our parentheses around it to make sure that alternation is kept distinct from those anchor tags on either side. Now we have a properly written regular expression, where both sides of the alternation require at least one character. Now, we didn't consider the possibility that you might have commas inside your number, delimiting the thousands places.
If you decide you wanted to tackle that on your own, the last movie of Chapter 8 gives you a regex that can help you get started. Instead, I want us to move on and look at currency. Currency is basically the same thing as a decimal number, with two major differences. There is currency symbol letting us know that it's currency, and there is typically two decimal places, or perhaps no decimal places at all. So, currency might look something like this. So how do we need to modify our regular expression so it will match this? Well, obviously we need a dollar sign at the beginning. We're going to want to make sure we escape that dollar sign, because remember, the dollar sign character is also our ending anchor.
So let's escape that as well, and voila, just like that, we've matched our currency. Now, what if we don't know what kind of currency it is? What if we want to allow for multiple kinds? Let's try working with the British Pound. So let's put in parentheses our dollar sign, and then use an alternation, and then we could use the British Pound here. Now let me just jump down here for a second, and let's type some sample data with the British Pound. The way that I get the British Pound on my Mac keyboard is to type Option+3, and then I can type in 498.10. Now I have some sample data.
Now up here, you could just type that same character again to get it to match, but I think it's a better practice, when you're using currency symbols besides the dollar sign, to use Unicode for them. And the Unicode for the British Pound is going be \u00A3, and now that matches it as well. How did I know that that was it? Well, I had to look it up. I had to go and research it, and find out what the Unicode was for the British Pound. Let's try one more. Let's try the Japanese Yen. So let's try to put a sample data down here. If I use Option+Y, that will give me the Yen symbol, and then 32.76.
So how do we match that? Same thing; up here, we could just simply type Option+Y up here, but it's better to use the Unicode symbol, which is going to be 00A5. The reason I wanted to show you this is because actually, for the Yen symbol, there are two Unicodes for it. There is a regular width Yen sign, and then there is what they call a full width Yen sign. So we want to make sure that we allow for that one as well, in case some used that alternate Unicode, and that is FFE5. Chances are, when you're matching something, they will have used the first one, and not the second one, but the point here is, with Unicode, you need to be careful.
You need to look it up, you need to find out what the code is for it, and you also need to make sure that there's not more than one encoding for it, because there are for several of these symbols. Last of all, let me just mention that if you're working with a currency that had its currency symbol at the end, you couldn't just simply put it here at the end, and have a properly written regular expression, because then there's the possibility that you would have a currency symbol at the beginning, and the end, and it would match both of those. So in order to have the proper alternation, you would need to have an alternation of the full expression. Either one possibility is it's at the beginning, followed by digits; second possibility is it's digits, followed by the symbols at the end.
There are currently no FAQs about Using Regular Expressions.
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.