Exercises






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 Figure. 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]

    [4] That is, search for text matching the regular expression "<CODE>(.*?)</CODE>"; for each successful match, write out the contents of capture group 1.

    [5] Hint: Read the entire text file into a string object by creating an if stream object to read the file and a basic_ostringstream object to build the string, and inserting the buffer returned by the ifstream's member function rdbuf() into the basic_ostringstream object.

  2. Now write another program that has a copy of the code of the search function in Figure. 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]

    [6] You'll have to write a callable type whose function call operator takes a match_results object and copies the first capture group to cout.

  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]

    [7] That is, search for text matching the regular expression "<(CODE|PRE)>(.*?)</\1>"; for each successful match, write out the contents of capture group 2.

Exercise 3

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.

[8] See Exercise 2 in Chapter 17 for a suitable regular expression.

Exercise 4

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]

[9] Hint: Write a regular expression that describes the separator, and use an iterator that shows the text that doesn't match the separator.

Exercise 5

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

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.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows