Regular Expressions

Assignment 1 Answers

  1. Find the string 'sound'.
    /sound
    
  2. Find the string 'sound' with an upper or lower case S.
    /[Ss]ound
    
  3. Find the string 'sound' when it is at the beginning of the line.
    /^sound
    
  4. Find the string 'sound' with an upper or lower case S when it is at the beginning of the line.
    /^[Ss]ound
    
  5. Find the string 'alive' when it is at the end of the line.
    /alive$
    
  6. Find lines that contain 'Buck' and 'Spitz' on the same line in that order.
    /Buck.*Spitz
    
  7. Find occurrences of one or two 'z's whether upper or lower case.
    /[zZ]\{1,2\}
    
  8. Change the string 'Buck' to 'Fido'.
    :%s/Buck/Fido/g
    
  9. Add ', the bad dog, ' after each occurrence of 'Spitz'.
    :%s/Spitz/Spitz, the bad dog,/g
    
    OR
    :%s/\(Spitz\)/\1, the bad dog,/g
    
  10. On lines that contain both 'Buck' and 'Spitz', add ', our hero, ' after 'Buck'.
    :/Buck.*Spitz/s/Buck/Buck, our hero,/g
    
    OR
    :/Buck.*Spitz/s/\(Buck\)/\1, our hero,/g