Most people get sick because they are infected with a virus. When a patient goes to the hospital for treatment, how can the doctor effectively and accurately detect whether the patient is infected with the corresponding virus is something that both the doctor and the patient are looking forward to. Pattern matching is widely used in practical applications, such as network intrusion detection, computer virus signature matching, DNA sequence matching and so on. The detection of virus infection can be realized by using pattern matching to compare the DNA of virus and the DNA of patient. Famous pattern matching algorithms include BF algorithm and KMP algorithm. BF algorithm is the simplest, most intuitive and easy to implement, and in the case of small string cost performance is also very small. Therefore, this paper selects BF algorithm to realize the virus infection detection mechanism. This paper first introduces the basic concept of pattern matching, the core idea of BF algorithm, algorithm thinking, algorithm steps, algorithm flow chart and algorithm description. Then the basic principle and realization of virus infection detection mechanism are described. Finally, an experimental test was carried out on VC++6.0 platform. The test results show that the proposed virus infection detection mechanism can effectively and accurately detect whether people are infected with the virus.