Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

Exercises

Exercise 1

For each of the following errors, write a simple test case containing the error, and try to compile it. In the error messages, look for the key words that relate to the error in the code.

  1. Attempting to construct a regex_iterator object by passing a pair of iterators whose character type is different from the regex_iterator type's character type
  2. Attempting to construct a regex_iterator object by passing a regular expression object whose element type or traits type is different from the regex_iterator type's element type or traits type
  3. Attempting to construct a regex_token_iterator object by passing a pair of iterators whose character type is different from the regex_-token_iterator type's character type
  4. Attempting to construct a regex_token_iterator object by passing a regular expression object whose element type or traits type is different from the regex_token_iterator type's element type or traits type
  5. Attempting to construct a regex_token_iterator object by passing a field specifier as a pointer to int instead of an array of int
  6. Attempting to decrement a regex_iterator object
  7. Attempting to decrement a regex_token_iterator object

Exercise 2

In the first part of this chapter, I mentioned that it's a little hard to reuse the brute-force loop. In this exercise, we look at a couple of possible approaches to reuse and at doing the same thing with regular expression iterators.

  1. Write a program that has a copy of the code of the search function in Example 19.8. Change the search function so that for a successful match, it shows the contents of the first capture group instead of the entire match. Now use the function to copy to cout all text that occurs between the tags "<CODE>" and "</CODE>" 4 in an HTML file of your choosing. 5
  2. Now write another program that has a copy of the code of the search function in Example 19.8. Change the search function into a template function with a template type parameter named Fn and an additional function call argument, Fn func. Also replace the code that shows the match by inserting it into cout with a call to func(match). Now use the function for the same search as in the preceding part of this exercise. 6
  3. Write a program that uses a regex_iterator object to do the same search.
  4. Write a program that uses a regex_token_iterator object to do the same search.
  5. Now change all four programs to copy to cout all text that occurs between the tags "<CODE>" and "</CODE>" or between the tags "<PRE>" and "</PRE>". 7

Exercise 3

  1. Use a pair of regex_iterator objects to search for valid host-names 8 in an HTML file, and use the utility function you wrote for Exercise 2 in Chapter 18 to show the contents of each successful match.

Exercise 4

  1. Write a program that uses a pair of regex_token_iterator objects to extract data fields from a comma-separated file. Don't forget to allow for spaces and tabs before and after each comma. 9

Exercise 5

  1. Write a program that puts the integer values 1 and 4 into a vector<int> and passes that vector as the field specifier in the constructor of a regex_token_iterator object. Use that object to search for your favorite regular expression. Now put the same values into an array of int, pass that array to the constructor, and repeat the search. What happens if the field index is higher than the index of the last capture group in the regular expression? What happens if you repeat a field index in the initializer?

Exercise 6

  1. HTML cross-references have the form <A HREF="reference"> text</A>" and <A NAME="reference">text</A>". The first is a link, and the second is the target of a link. In both cases, the reference is in quotes. Write a program that uses a pair of regex_token_iterator objects to search for cross-references in an HTML file and shows, for each cross-reference, either "HREF=" or "NAME=", as appropriate, followed by text of the reference.

  • + Share This
  • 🔖 Save To Your Account