Matching a range of characters

It is possible to match a range of characters instead of just one. This can add some flexibility to the pattern:

  • [A-Z] matches all capital letters
  • [a-z] matches all lowercase letters
  • [0-9] matches all digits
....
lower_case_letter = re.compile("[a-z]")
if lower_case_letter.search("a"):
print("'a' is a match")
if lower_case_letter.search("B"):
print("'B' is a match")
if lower_case_letter.search("123 A B 2"):
print("'123 A B 2' is a match")

digit = re.compile("[0-9]")
if digit.search("1"):
print("'a' is a match")
if digit.search("342"):
print("'a' is a match")
if digit.search("asdf abcd"):
print("'a' is a match")
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset