import re

# Start and end with letter or number
# pattern = "^[a-z0-9].*[a-z0-9]$"

# Start and end with letter or number, and consist only letter,
# number, dot, and hyphens
pattern = r"^[a-z0-9][a-z0-9. -]*[a-z0-9]$"


string_one = "hello world"
string_two = "hello ^ world 2"
string_three = "hello world *"
string_four = "Hello - World 9"
string_five = "hello - world 9"
string_six = "2hello.world 9"


if re.search(pattern, string_one):
    print ("matched with string one: ",string_one)

if re.search(pattern, string_two):
    print("matched with string two: ",string_two)

if re.search(pattern, string_three):
    print ("matched with string three: ",string_three)

if re.search(pattern, string_four):
    print("matched with string four: ", string_four)

if re.search(pattern, string_five):
    print("matched with string five: ", string_five)

if re.search(pattern, string_six):
    print("matched with string six: ", string_six)