fork download
  1. import re
  2.  
  3. # Start and end with letter or number
  4. # pattern = "^[a-z0-9].*[a-z0-9]$"
  5.  
  6. # Start and end with letter or number, and consist only letter,
  7. # number, dot, and hyphens
  8. pattern = r"^[a-z0-9][a-z0-9. -]*[a-z0-9]$"
  9.  
  10.  
  11. string_one = "hello world"
  12. string_two = "hello ^ world 2"
  13. string_three = "hello world *"
  14. string_four = "Hello - World 9"
  15. string_five = "hello - world 9"
  16. string_six = "2hello.world 9"
  17.  
  18.  
  19. if re.search(pattern, string_one):
  20. print ("matched with string one: ",string_one)
  21.  
  22. if re.search(pattern, string_two):
  23. print("matched with string two: ",string_two)
  24.  
  25. if re.search(pattern, string_three):
  26. print ("matched with string three: ",string_three)
  27.  
  28. if re.search(pattern, string_four):
  29. print("matched with string four: ", string_four)
  30.  
  31. if re.search(pattern, string_five):
  32. print("matched with string five: ", string_five)
  33.  
  34. if re.search(pattern, string_six):
  35. print("matched with string six: ", string_six)
Success #stdin #stdout 0.03s 9496KB
stdin
Standard input is empty
stdout
matched with string one:  hello world
matched with string five:  hello - world 9
matched with string six:  2hello.world 9