Trying to create a function which with a given a regular expression which holds all the legal characters, a string will be checked if it only contains those characters.
For example
import re
legal_characters = r'[\*\-]' # Matches asterisc and dash characters
def is_legal(test_string):
if re.match(legal_characters, test_string):
print("Legal")
else:
print("Not legal")
is_legal("***---123") # should print "Not legal"
is_legal("AbC123") # should print "Not legal"
is_legal("*-*-*") # should print "Legal"
output:
Not legal
Not legal
Not legal
I do not really understand why. Could someone please explain?