In addition to @cricket_007's comment (no need for .*
, as long as you switch to re.search
which doesn't implicitly insert a start of line anchor at the front), searching for bin with no other qualifiers is likely to get a lot of false positives. And using grouping parens makes the check more expensive (it has to store capture groups). Lastly, you should always use raw strings for regexes, or it will eventually bite you. Put together, you could use the regex with if re.search(r'\b[Bb]in\b', line):
to enforce word boundaries, avoid unnecessary capture, and still do what you intend.
You could even optimize it a bit by pre-compiling the regex (Python caches compiled regular expressions, but it still involves executing Python level code to check the cache every time; a compiled object goes straight to C with no delays).
This lets you simplify to:
import re
# Compile and store bound method with useful name; use character classes
# to avoid capture of B/b, and word boundary assertions to avoid capturing
# longer words containing name, e.g "binary" when you want bin
hasuser = re.compile(r'\b[Bb]in\b').search
#open the auth.log for reading using with statement to close file deterministically
with open('auth.log') as myAuthlog:
# Filter for lines with the specified user (in Py3, would need to wrap
# filter in list or use sum(1 for _ in filter(hasuser, myAuthlog)) idiom
loginattempts = len(filter(hasuser, myAuthlog))
print "User attempted to log in", loginattempts, "times"