The perfect regular expression... (^|\n)(\s+)?PermitRootLogin no([\s#]+)?(#(.*))?(?=\n|$) even more fun - this or that (^|\n)(\s+)?(PermitRootLogin no|PermitRootLogin logan)([\s#]+)?(#(.*))?(?=\n|$)