You can specify that a string only matches when it occurs at the beginning of a line with circumflex (^). You can specify that a string only matches when it occurs at the end of a line with $.
Regular Expression
Match
Not a Match
^Sir
Sir, Sir John
Dear Sir,
John$
John, Sir John
John Smith
^Sir$
Sir
Dear Sir, Sir John
^.$
a (any line with only one character)
aa (any line with more or less than one character)