Exercises






Exercises

Exercise 1

In this exercise, we look at three approaches to displaying the results of regular expression searches. As we've been doing, we'll write each result directly to cout and will then use regex_match::format to insert characters from each match into an output iterator that writes to cout; finally, we'll use regex_replace to manage the search loop for us.

  1. Write a program that searches for text in the form "name: first-name last-name" and inserts the contents of each successful match into cout with the last name first, followed by a comma, followed by the first name.

  2. Write a program that uses a pair of regex_iterator objects to do the search and calls iter->format to write all the desired text to cout. You can use an iterator object ostream_iterator<char> out(cout, "") to insert individual characters into cout.

  3. Write a program that uses regex_replace with the flag format_no_copy to search for matches and write out their contents.

Exercise 2

Write a program that searches for one occurrence of text that matches a hostname[5] and uses regex_match::format to create a string object with an HTML link to that host. For example, the hostname http://www.petebecker.com would be converted to <A HREF="http://www.petebecker.com">http://www.petebecker.com</A>.

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

Exercise 3

Write a program that copies an input file and replaces every occurence of text that matches a hostname with an HTML link to that host.

Exercise 4

Write a program that searches an input file for text that matches a hostname and for each match writes a line of text with an HTML link to that host into an output file.

Exercise 5

Write a program that searches the standard input for text that matches a hostname and for each match writes a line of text with an HTML link to that host to the standard output.



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