Exercises






Exercises

Exercise 1

The best way to learn about the details of regular expressions is to use them. If you're confused by any of the examples in this chapter, try them out with the various match functions. Be careful of backslashes: In a C-style string, you need two for each one in the regular expression.

Exercise 2

For each of the following target sequences, write a regular expression using the ECMAScript grammar and a regular expression using the grep grammarthey will often be the samethat matches it and nothing else:

  1. The letter 'a'

  2. The letter 'b'

  3. The letter 'a' followed by the letter 'b'

  4. Any character

  5. Any character followed by any character

  6. Any vowel

  7. Any character that is not a vowel

  8. Any of the characters 'b', 'c', 'd', 'e', 'f', 'g', 'h'

  9. Any letter of the alphabet

  10. The character '{'

  11. The character '\'

  12. Any character followed by the same character

  13. Any letter except 'Q'

  14. Any letter except 'Q' and 'x'

  15. Any number of occurrences of the letter 'a'

  16. Any number of occurrences of the letter sequence "ab"

  17. Any number of occurrences of either of the letters 'a' and 'b'

  18. Zero or one occurrence of either of the letters 'a' and 'b'

  19. One or more occurrences of either of the letters 'a' and 'b'

  20. Seventeen or more occurrences of either of the letters 'a' and 'b'

  21. One of the three HTML tags "<EM>", "<CODE>", and "<PRE>"; don't make allowances for lowercase characters

  22. One of those same three HTML tags, followed by an arbitrary sequence of characters that does not include a '<', followed by the corresponding closing tag ("</EM>", "</CODE>", or "</PRE>")

Exercise 3

How many ways can you think of to write a regular expression that matches any of the target sequences "0", "1", and "2"?

Exercise 4

How many ways can you think of to write a regular expression that matches a single hexadecimal digitany decimal digit or any of the letters 'a' tHRough 'f', either lowercase or uppercase?

Exercise 5

  1. Write a regular expression that matches a sequence of three characters followed by the same three characters in the same order.

  2. Write a regular expression that matches a sequence of three characters followed by the same three characters in reverse order.



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