Regular Expression Exercises

  1. Parse Stock prices. Create a function that will decode the old-style fractional stock price. The price can be a simple floating point number or it can be a fraction, for example, 4 5/8.

    Develop two patterns, one for numbers with optional decimal places and another for a number with a space and a fraction. Write a function that accepts a string and checks both patterns, returning the correct decimal price for whole numbers (e.g., 14), decimal prices (e.g., 5.28) and fractional prices (27 1/4).

  2. Parse Dates. Create a function that will decode a few common American date formats. For example, 3/18/87 is March 18, 1987. You might want to do 18-Mar-87 as an alternative format. Stick to two or three common formats; otherwise, this can become quite complex.

    Develop the required patterns for the candidate date formats. Write a function that accepts a string and checks all patterns. It will return the date as a tuple of ( year, month, day ).