Regular expressions

Exercise 1.
Create an application in Python which can print all the lines from Shelley’s Complete Poems  that contain the following:

  • lines containing the word “fire”
  • lines containing the either the word “sun” or to word “moon”. Use a single regular expression to identify these lines.
  • lines which contain either the singular or the plural form of “star”.
  • lines which contain a question mark.
  • Lines ending in the character sequence “ain”.
  • Lines which contain at least two words that begin with “br”
  • either the singular or the plural form of “leaf”.


Exercise 2.
Download the file “Ulysses.txt“. It is the full text of James Joyce’s novel Ulysses. Write regular expressions to retrieve texts fragments with the following characteristics:

  • Text fragments containing a year (e.g. the sentence “What reflection concerning the irregular sequence of dates 1884, 1885, 1886, 1888, 1892, 1893, 1904 did Bloom make before their arrival at their destination?”)
  • Text fragments in which Joyce chose the dramatic form, or, more specifically, lines which begin with the name of a speaker in capitals, followed directly by a colon.
  • Lines which consist of less than 30 characters.
  • Lines containing surnames beginning with “O'”, followed by apostrophe (e.g. O’Connell, O’Brian)

Solutions to these exercises