At risk of adding yet another answer to a question which has many already, here is how I think that a regular expression parser is best used here:
import re
the_id = 1740
with open("test.txt") as f:
for line in f:
match = re.search("id\s+(\d+)\s*$", line)
if match and the_id == int(match.group(1)):
print(line, end='')
This gives:
2012-02-03 18:35:34 SampleClass3 [WARN] missing id 1740
What you are doing here is using the parser to look for lines which end with the following: "id", followed by whitespace, followed by one or more digits (which you capture in a group), optionally followed by any amount of whitespace.
The captured group is then converted to int and compared with the id.
Incidentally, the id is stored in variable called the_id, because id is the name of a builtin function so is not a good choice of variable name (interferes with use of the builtin).
UPDATE
The asker has now clarified that the ID can appear in the middle of the line, not necessarily at the end.
This can easily be handled by a simple tweak to the regular expression. Changing the relevant line in the above code to:
match = re.search("id\s+(\d+)", line)
now removes any check on what should come after the digits.
Because the + meaning "one or more" is also greedy (that is, it matches the part of the pattern to which it relates as many times as possible), the whole of the ID is matched by the bracketed group, without need to specify anything about what follows it.
Given the input file
2012-02-03 18:35:34 SampleClass6 [INFO] everything normal for id 174025851
2012-02-03 18:35:34 SampleClass4 [FATAL] system problem at id 1991740254
2012-02-03 18:35:34 SampleClass3 [DEBUG] detail for id 1304807656
2012-02-03 18:35:34 SampleClass3 [WARN] missing id 1740
2012-02-03 19:11:02 id 1740 SampleClass5 [TRACE] verbose detail
this will now output:
2012-02-03 18:35:34 SampleClass3 [WARN] missing id 1740
2012-02-03 19:11:02 id 1740 SampleClass5 [TRACE] verbose detail
remodule then wouldn'tre.search(r'\b1740\b',line)suffice?