'$ matches the end of string, and just before the terminating'

def test_dollar_matches_twice(self):

    pattern = re.compile('$')
    self.assertEqual(pattern.sub('#', 'a\nb\n'), 'a\nb#\n#')
    self.assertEqual(pattern.sub('#', 'a\nb\nc'), 'a\nb\nc#')
    self.assertEqual(pattern.sub('#', '\n'), '#\n#')
    pattern = re.compile('$', re.MULTILINE)
    self.assertEqual(pattern.sub('#', 'a\nb\n'), 'a#\nb#\n#')
    self.assertEqual(pattern.sub('#', 'a\nb\nc'), 'a#\nb#\nc#')
    self.assertEqual(pattern.sub('#', '\n'), '#\n#')
