Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Matching decimal numbers and currency

From: Using Regular Expressions

Video: Matching decimal numbers and currency

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.

Matching decimal numbers and currency

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.

Show transcript

This video is part of

Image for Using Regular Expressions
Using Regular Expressions

59 video lessons · 11682 viewers

Kevin Skoglund
Author

 
Expand all | Collapse all
  1. 2m 18s
    1. Welcome
      56s
    2. Using the exercise files
      1m 22s
  2. 19m 55s
    1. What are regular expressions?
      3m 20s
    2. The history of regular expressions
      6m 40s
    3. Regular expression engines
      2m 44s
    4. Installing an engine
      4m 5s
    5. Notation conventions and modes
      3m 6s
  3. 21m 23s
    1. Literal characters
      6m 39s
    2. Metacharacters
      2m 1s
    3. The wildcard metacharacter
      4m 31s
    4. Escaping metacharacters
      4m 53s
    5. Other special characters
      3m 19s
  4. 31m 26s
    1. Defining a character set
      5m 49s
    2. Character ranges
      4m 49s
    3. Negative character sets
      4m 53s
    4. Metacharacters inside character sets
      5m 12s
    5. Shorthand character sets
      6m 30s
    6. POSIX bracket expressions
      4m 13s
  5. 36m 38s
    1. Repetition metacharacters
      7m 17s
    2. Quantified repetition
      6m 59s
    3. Greedy expressions
      6m 27s
    4. Lazy expressions
      6m 46s
    5. Using repetition efficiently
      9m 9s
  6. 20m 24s
    1. Grouping metacharacters
      4m 14s
    2. Alternation metacharacter
      4m 54s
    3. Writing logical and efficient alternations
      7m 33s
    4. Repeating and nesting alternations
      3m 43s
  7. 19m 19s
    1. Start and end anchors
      7m 21s
    2. Line breaks and Multiline mode
      4m 41s
    3. Word boundaries
      7m 17s
  8. 23m 33s
    1. Backreferences
      8m 57s
    2. Backreferences to optional expressions
      3m 51s
    3. Finding and replacing using backreferences
      7m 16s
    4. Non-capturing group expressions
      3m 29s
  9. 32m 31s
    1. Positive lookahead assertions
      6m 39s
    2. Double-testing with lookahead assertions
      7m 16s
    3. Negative lookahead assertions
      6m 10s
    4. Lookbehind assertions
      6m 26s
    5. The power of positions
      6m 0s
  10. 13m 13s
    1. About Unicode
      4m 19s
    2. Unicode in regular expressions
      4m 41s
    3. Unicode wildcards and properties
      4m 13s
  11. 1h 55m
    1. How to use this chapter
      5m 38s
    2. Matching names
      6m 33s
    3. Matching postal codes
      8m 54s
    4. Matching email addresses
      5m 0s
    5. Matching URLs
      8m 1s
    6. Matching decimal numbers and currency
      6m 45s
    7. Matching IP addresses
      7m 10s
    8. Matching dates
      7m 49s
    9. Matching times
      8m 59s
    10. Matching HTML tags
      8m 34s
    11. Matching passwords
      6m 49s
    12. Matching credit card numbers
      9m 36s
    13. Finding words near other words
      6m 38s
    14. Formatting with Search and Replace, pt. 1
      7m 22s
    15. Formatting with Search and Replace, pt. 2
      4m 15s
    16. Formatting with Search and Replace, pt. 3
      7m 10s
  12. 47s
    1. Goodbye
      47s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Using Regular Expressions.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.