The re module is a unique module for matching strings in Python. Many functions provided in this module are implemented based on regular expressions, and regular expressions are used to perform fuzzy matching on strings and extract the string parts they need. All languages are common. note:
Use the re module to define a pattern string for verifying dangerous characters, verify two paragraphs of text, and output the verification result:
import re
pattern = r'(hacker)|(Capture)|(monitor)|(Trojan)'
about ='I am a programmer, I like to read books about hackers, and I want to study Trojan'
match =re.search(pattern,about)if match == None:print(about,'@Safety!')else:print(about,'@Dangerous words appeared!')
about ='I am a programmer, I like to read computer books and develop websites'
match =re.search(pattern,about)if match == None:print(about,'@Safety!')else:print(about,'@Dangerous words appeared!')
result:
I am a programmer, I like to read books about hackers, and I want to study Trojan @ There are dangerous words!
I am a programmer, I like to read computer books, and I like to develop websites @安全!
The above is the whole content of this article, I hope it will be helpful to everyone's study.
Recommended Posts